avoid interleaving pyuwsgi threadstate #2660
Closed
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.
resolves #2659
so there's quite a lot going on here so I'll try to explain the change
NULL
threadstateuwsgi_python_master_fixup
which callsUWSGI_RELEASE_GIL
(expanded toPyEval_SaveThread
-- which drops the GIL and sets threadstate toNULL
)uwsgi_setup
uwsgi_setup
was returning,PyThreadState_Swap
was restoring the pyuwsgi threadstate (in both the original and worker processes)PyThreadState_Swap
)PyEval_RestoreThread
would attempt totake_gil
and then block forever on the GIL mutex (despite it actually holding it? due to the fork state from the parent process)PyThreadState_Swap
(it now additionally manages GIL state: unlocking the previous threadstate and locking the new threadstate)PyThreadState_Swap
showed a suspiciousswapping from oldts=123123 to newts=123123
(swapping from its own threadstate to itself?)uwsgi_setup
we never give control back to the originalpyuwsgi
threadstate avoiding theSwap
dance entirely!