Skip to content

Make concurrent_writer::continue_writing_ atomic (fixes #3464) - #3465

Open
viftode4 wants to merge 1 commit into
stan-dev:developfrom
viftode4:fix-concurrent-writer-flag-race
Open

viftode4 wants to merge 1 commit into
stan-dev:developfrom
viftode4:fix-concurrent-writer-flag-race

Conversation

@viftode4

Copy link
Copy Markdown

Summary

Make concurrent_writer::continue_writing_ a std::atomic<bool>. Fixes #3464.

Intended Effect

wait() writes the flag from the calling thread while the worker reads it in its loop condition. That is a data race under the C++ memory model, and ThreadSanitizer reports it for a writer that is constructed and then waited on. The change removes the report. Behaviour is otherwise unchanged. wait() still sets the flag and then joins. The worker loop is unchanged.

On @WardBrian's question in the issue: on x86 I do not expect a visible failure. The worker's loop has opaque calls (try_pop, yield), so the compiler is unlikely to hoist the load. The race is formally undefined behaviour, and it makes TSan builds of any Stan program using threaded output fail, so a one-line atomic seemed cheap. Close this if you would rather not take it.

How to Verify

Reporter's program from #3464 (construct a concurrent_writer, call wait()), built with -O1 -g -fsanitize=thread -DSTAN_THREADS, GCC 13.3, Ubuntu 24.04 x86-64, unmodified develop against this branch, 20 runs each:

  • before: data race reported 20/20
  • after: 0/20

I did not run the repo's unit-test suite or make cpplint.

Side Effects

None expected. Default seq_cst loads and stores on a bool flag.

Documentation

None.

Copyright and Licensing

Copyright holder: viftode4 (the account submitting this).

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

AI disclosure

This change and the TSan check were made by an AI agent (Claude, running as an inhabitant of the SLIME project) working for the account owner. The reporter's reproduction was reused as is.

🤖 Generated with Claude Code

wait() writes the flag from the calling thread while the worker thread reads it in its loop condition. ThreadSanitizer reports this as a data race on an idle writer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@WardBrian

Copy link
Copy Markdown
Member

@SteveBronder while adding the pathfinder concurrent writer in #3325 were there any specific benchmarks you were running that would be good to re-run with this change?

@WardBrian

Copy link
Copy Markdown
Member

@viftode4 Thanks for the PR. Would you mind removing the co-authored-by bit on the commit? Our AI policy requires the human contributor to take responsibility for the patch (which seems easy, in this case)

@bob-carpenter

Copy link
Copy Markdown
Member

@viftode4: Sorry about this, but we've been hashing out our AI policy. None of this is about your PR per se.

@WardBrian If our policy is going to be to forbid Claude attributions in headers, maybe we could cut them off at the pass by blocking commits that are by "X and Claude". Or maybe some kind of hook in PRs that looks for this boilerplate?

I don't know if there's a way to get commits made by Claude to not include the self attribution, but if so, we should indicate that to users if this is our policy by pointing them to Claude's doc.

It would be great if these automatic rejections could point to an explicit piece of our AI policy. It's not clear to me, at least, that this PR violates our policies.

@bob-carpenter

Copy link
Copy Markdown
Member

Another thing to flag in this PR is the copyright. Are we running into legal issues accepting PRs attributed to GitHub accounts? That's not how copyright works. It gets assigned to the person who authored the content or to whatever legal entity they assign their copyright to. I do not believe GitHub accounts are legitimate copyright holders. This is not the only case of this we have as not all of our developers have identified themselves "in real life."

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.

ThreadSanitizer reports a shutdown race in concurrent_writer

3 participants