Skip to content

Avoid scheduling duplicate cleanup pending calls while draining (#535) - #538

Open
dianatofficial wants to merge 1 commit into
python-greenlet:masterfrom
dianatofficial:fix/cleanup-queue-pending-call-535
Open

dianatofficial wants to merge 1 commit into
python-greenlet:masterfrom
dianatofficial:fix/cleanup-queue-pending-call-535

Conversation

@dianatofficial

Copy link
Copy Markdown

Fixes #535.

Cause

ThreadState_DestroyNoGIL::AddToCleanupQueue scheduled PendingCallback_DestroyQueue whenever thread_states_to_destroy.size() == 1. Because PendingCallback_DestroyQueue pops an entry under thread_states_to_destroy_lock before dropping the lock to call DestroyOne(), a second thread exiting during DestroyOne() makes size() == 1 again and schedules a second Py_AddPendingCall while Py_MakePendingCalls() is already running.

On CPython 3.11, scheduling a pending call while make_pending_calls() is executing leaves the eval breaker set and causes sys.settrace() to spin indefinitely inside DO_TRACING if DestroyOne() executes Python code (such as weakref callbacks).

Fix

  • Add cleanup_scheduled (guarded by thread_states_to_destroy_lock) on GreenletGlobals.
  • AddToCleanupQueue schedules PendingCallback_DestroyQueue only when !cleanup_scheduled and sets cleanup_scheduled = true if AddPendingCall succeeds.
  • PendingCallback_DestroyQueue clears cleanup_scheduled = false inside the same critical section where it observes thread_states_to_destroy.empty().
  • Add test_cleanup_during_pending_call_with_trace_no_livelock in src/greenlet/tests/test_tracing.py to exercise a second thread queuing cleanup during a main-greenlet weakref callback under sys.settrace().

Verification

Fork CI run (46395761190):

  • Passed full test suite + doctest on CPython 3.10, 3.11, 3.12, 3.13, 3.15-dev (Ubuntu & Windows), 3.11/3.12 (windows-11-arm), and 3.14 (macos-latest), including test_cleanup_during_pending_call_with_trace_no_livelock, as well as CodeQL.
  • pylint-4.1.5 rated all modified test files cleanly (the 3.14 Ubuntu lint job failed only on pre-existing src/greenlet/tests/leakcheck.py:300 R1716).
  • Free-threaded Windows jobs (3.14t, 3.15t-dev) hit the pre-existing test_issue_245_reference_counting_subclass_threads abort seen on master.

@dianatofficial

Copy link
Copy Markdown
Author

CI verification summary for run #46397101637:

  • All standard-GIL test jobs across Python 3.10, 3.11, 3.12, 3.13, 3.14 (macOS, Windows, ARM64), and 3.15-dev, as well as \CodeQL, passed.
  • \ est (3.14, ubuntu-latest)\ passed its tests and doctests and failed only on a pre-existing \pylint 4.1.5\ \R1716\ lint check at \src/greenlet/tests/leakcheck.py:300.
  • The free-threaded \3.14t\ and \3.15t-dev\ failures/6h timeouts match pre-existing \upstream/master\ baseline behavior (stack traces under \ aulthandler\ show the main thread waiting inside \wait_for_pending_cleanups()\ / \collect()\ at \src/greenlet/init.py:106\ on worker threads at \ est_issue_245_reference_counting_subclass_threads\ / \src/greenlet/tests/test_greenlet.py:855), unrelated to the \cleanup_scheduled\ pending-call guard.

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.

Main thread livelocks on CPython 3.11 when a second thread dies while PendingCallback_DestroyQueue runs Python code

1 participant