GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-120321: Lock gen_send by Fidget-Spinner · Pull Request #120327 · python/cpython

Conversation

@Fidget-Spinner

@Fidget-Spinner Fidget-Spinner commented Jun 10, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Adds per-object lock for gen_send to fix crashes.

  • Issue: SIGSEGV with generators in free-threaded build #120321

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Fidget-Spinner added 2 commits June 11, 2024 02:28
@Fidget-Spinner
Lock gen_send
4f08322
@Fidget-Spinner
Add comment
ab453ea
@Fidget-Spinner
Fidget-Spinner requested a review from markshannon as a code owner June 10, 2024 18:30
@bedevere-app bedevere-app Bot mentioned this pull request Jun 10, 2024
SIGSEGV with generators in free-threaded build #120321
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 10, 2024
@Fidget-Spinner Fidget-Spinner added topic-free-threading needs backport to 3.13 only security fixes and removed awaiting core review labels Jun 10, 2024
Fidget-Spinner and others added 2 commits June 11, 2024 02:38
@Fidget-Spinner
Create test_generators.py
3d23ada
@blurb-it
📜🤖 Added by blurb_it.
bfe9ead
@markshannon

markshannon commented Jun 11, 2024

Copy link
Copy Markdown
Member

What's the root cause of #120321?
Until you know that, how do you know this fixes the problem?

This feels like slapping a lock on something and hoping it works.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 11, 2024

Copy link
Copy Markdown
Member Author

What's the root cause of #120321? Until you know that, how do you know this fixes the problem?

The crash's root cause is here https://github.com/python/cpython/pull/120327/files#diff-2a0c449b68605ebd0872fd232e60ce7e838a77782a6d2e364764f99514fb508aR219

Those 3 lines can race and cause tstate->exc_info along with gi_exc_state to become invalid.

I locked the whole thing because TSAN is warning not just those few lines. It warns that the _PyFrame_StackPush and all the other stack accessor functions in there are racing as well. Though I don't think that results in a crash.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Jun 11, 2024

Copy link
Copy Markdown
Member

What about all the other places that tstate->exc_info is updated when we push or pop a generator frame?

I suspect that this doesn't crash only because specialization is turned off for free-threading:

def gen():
    while True:
        yield

def my_next(it):
    for val in it:
        return value
    raise StopIteration

it = gen()
with concurrent.futures.ThreadPoolExecutor() as executor:
    while True:
        _ = executor.submit(lambda: my_next(it))
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 11, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I suspect that this doesn't crash only because specialization is turned off for free-threading:

Yeah the entire gen_send has multiple races in multiple places. Likewise for the specializing interpreter. So the entire thing needs to be locked (what the GIL does at the moment).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Jun 11, 2024

Copy link
Copy Markdown
Member

gen_send isn't the problem. It contains an instance of the problematic code, but there are instances in bytecodes.c in _SEND and _FOR_ITER_GEN`.

Pushing and popping generator frames needs to be locked. The best way to do that would be to move popping and pushing into its own helper function.

Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 11, 2024

Copy link
Copy Markdown
Member Author

I agree that pushing and popping frames need fixing, but gen_send is still problematic even with that. It has non-atomic accesses of fields on tstate and the frame which another thread could be concurrently modifying. In fact even after factoring out the generator frame pushing and locking just that, I still get crashes from the other issues in gen send.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Jun 11, 2024

Copy link
Copy Markdown
Member

OOI, what are the other crashes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 12, 2024

Copy link
Copy Markdown
Member Author

A selection of them after I've locked just the frame pushing

python: Objects/genobject.c:235: gen_send_ex2_unlocked: Assertion `gen->gi_exc_state.previous_item == NULL' failed

./Include/internal/pycore_frame.h:313: _PyFrame_GetGenerator: Assertion `frame->owner == FRAME_OWNED_BY_GENERATOR' failed.

./Include/internal/pycore_frame.h:77: _PyFrame_GetCode: Assertion `PyCode_Check(f->f_executable)' failed.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DinoV

DinoV commented Jun 12, 2024

Copy link
Copy Markdown
Contributor

I wonder if we really only need the locks up to the transition to gen->gi_frame_state = FRAME_EXECUTING;. That would seem to work better with fixing SEND in the opcode as well as doing the unlocking there isn't going to be as easy.

I think other than the asserts that only leaves if (FRAME_STATE_SUSPENDED(gen->gi_frame_state)) { as the lone racing read. I'm not sure that this is fully really safe even w/ the GIL though. It seems like _PyEval_EvalFrameDefault wouldn't release the GIL between yielding a value and getting back to here, but something that's hooked the evaluation function could indeed release the GIL. So this is really already racing against another thread resuming the generator already (and you shouldn't get an interpreter crash, just odd results from trying to run a generator from multiple threads at the same time, which isn't really a sane thing to do anyway).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 2 commits July 13, 2024 23:26
@Fidget-Spinner
Merge remote-tracking branch 'upstream/main' into fix_gens
3996575
@Fidget-Spinner
lock SEND as well
24e34e3
@colesbury colesbury mentioned this pull request Nov 4, 2024
gh-126366: Make native generators thread safe #126371
Closed
@vstinner vstinner mentioned this pull request Nov 6, 2024
Abort in free-threaded build due to mutation of ChainMap of a Counter in threads #126366
Closed
@vstinner

vstinner commented Nov 6, 2024

Copy link
Copy Markdown
Member

What's the status of this PR? It now has multiple merge conflicts.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner Fidget-Spinner closed this Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

needs backport to 3.13 only security fixes topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Fidget-Spinner @markshannon @DinoV @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.