Skip to content

Fix statebuffer race#669

Closed
josephdviviano wants to merge 3 commits into
Farama-Foundation:mainfrom
josephdviviano:fix-statebuffer-race
Closed

Fix statebuffer race#669
josephdviviano wants to merge 3 commits into
Farama-Foundation:mainfrom
josephdviviano:fix-statebuffer-race

Conversation

@josephdviviano

Copy link
Copy Markdown

The test failing in #668 is actually due to a flakey race condition that has failed in previous runs of CI - on master — on Windows, in CI run 20594583659 from December 30.

I think, in unordered mode, write_idx_.fetch_add(1) assigns a circular buffer position, but the actual data write happens after the position is claimed. Another thread can finish its write and increment count_ past the batch boundary, causing collect() to read a position that the first thread hasn't written to yet.

I was able to reproduce on my machine with the following test on the current codebase, but it should pass currently.

@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

Thanks for the PR @josephdviviano, I was in the process of completely rewriting the vectorizer to fix this problem in #652 however didn't observe significant throughput increase so that sadly stalled.
We can probably merge the MR PR without this fix but it is now a priority for me

@josephdviviano

Copy link
Copy Markdown
Author

No problem! Aside from style any changes required?

The sync_env_ids variable was extracted from info but never used,
masking a missing sanity check. Add assertions that sync env IDs
are always [0, 1, ..., N-1] after reset and each step, validating
the baseline the async comparison relies on.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

#652 has been merged and has resolved this issue though further optimisation is warranted

@josephdviviano

josephdviviano commented May 29, 2026 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants