Skip to content

Conversation

@Dan-Flores
Copy link
Contributor

@Dan-Flores Dan-Flores commented Dec 20, 2025

This PR consolidates the logic used to flush and close AVIOContext, and should fix the flaky test bug described in #724.

The issue is related to the member variables avFormatContext_ and avioContextHolder_ pointing to the same AVIOContext object, and the logic of close_avio.

avFormatContext_->pb = avioContextHolder_->getAVIOContext();

In close_avio, if avioContextHolder_ is not defined (if using to_file), the AVIOContext pointer is correctly set to nullptr, preventing double frees in the member variables destructors. Otherwise, it will be destroyed in the member variable destructors. In those destructors, both will attempt to free the same address, as it is never set to a nullptr:
AVIOContextHolder::~AVIOContextHolder() {
if (avioContext_) {
av_freep(&avioContext_->buffer);
}
}

using UniqueEncodingAVFormatContext = std::unique_ptr<
AVFormatContext,
Deleter<AVFormatContext, void, avformat_free_context>>;

The updated closeAVIOContext function avoids this by

  • Returning early if avFormatContext_ or avFormatContext_->pb are set to nullptr (so it is safe to call multiple times).
  • Calls avio_close on the AVIOContext pointer if avioContextHolder_ is not defined (when using to_file). Otherwise, the reference in avFormatContext_ will be set to nullptr.
    • During avioContextHolder's destructor, the AVIOContext will be freed (only once!).

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 20, 2025
@Dan-Flores Dan-Flores changed the title Test refactoring closeAVIOContext logic Refactor logic to close AVIOContext Jan 6, 2026
@Dan-Flores Dan-Flores marked this pull request as ready for review January 6, 2026 05:19
Copy link
Contributor

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Dan-Flores , let's try this. The refactor is useful regardless of the logic change!

}
}

avFormatContext->pb = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check my understanding that the main logic difference is that we now set avFormatContext->pb = nullptr; after the if (!avioContextHolder) { ...} block, and not inside it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, after and outside of the condition.

@Dan-Flores Dan-Flores merged commit f91d24a into meta-pytorch:main Jan 6, 2026
81 of 82 checks passed
@Dan-Flores Dan-Flores deleted the generalize_close_avio branch January 6, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants