Skip to content

Fix AttributeError on shutdown when threading.Thread is replaced mid-tracking#910

Merged
godlygeek merged 1 commit into
bloomberg:mainfrom
swaroski:fix/856-gevent-thread-swap
Apr 28, 2026
Merged

Fix AttributeError on shutdown when threading.Thread is replaced mid-tracking#910
godlygeek merged 1 commit into
bloomberg:mainfrom
swaroski:fix/856-gevent-thread-swap

Conversation

@swaroski
Copy link
Copy Markdown
Contributor

Closes #856

Summary

  • Tracker.__enter__ instruments threading.Thread by setting _name and _ident descriptors on the class.
  • If the tracked script replaces threading.Thread during tracking (notably gevent.monkey.patch_all()),
    Tracker.__exit__ previously looked up threading.Thread again and tried to delattr from a different class,
    raising AttributeError.
  • Now the patched class is captured on entry and cleaned up on exit, with a defensive AttributeError suppression.

Test plan

  • New regression test in tests/unit/test_tracker.py swaps threading.Thread mid-context.
  • Reproduced the original gevent crash from the issue, confirmed fixed against the rebuilt extension.

Copy link
Copy Markdown
Contributor

@godlygeek godlygeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Like the other PRs, should only have the last commit, and needs a Signed-off-by line with your real name in it.

Comment thread tests/unit/test_tracker.py Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.48%. Comparing base (015e6f0) to head (924935d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #910      +/-   ##
==========================================
+ Coverage   92.46%   92.48%   +0.02%     
==========================================
  Files          99       99              
  Lines       11748    11754       +6     
  Branches      427      427              
==========================================
+ Hits        10863    10871       +8     
+ Misses        885      883       -2     
Flag Coverage Δ
cpp 92.48% <100.00%> (+0.02%) ⬆️
python_and_cython 92.48% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@swaroski swaroski force-pushed the fix/856-gevent-thread-swap branch from 610a976 to b59a906 Compare April 28, 2026 20:54
@swaroski
Copy link
Copy Markdown
Contributor Author

@godlygeek addressed: switched test to monkeypatch fixture, added
Signed-off-by with full name.

Tracker now records the `threading.Thread` subclass it patched on entry
and removes its instrumentation from that same class on exit, ignoring
`AttributeError` defensively. Fixes a crash seen when scripts call
`gevent.monkey.patch_all` while tracking is active, modifying what
`threading.Thread` refers to underneath us.

Signed-off-by: Suyog Bhise <bhise.suyog@gmail.com>
@godlygeek godlygeek force-pushed the fix/856-gevent-thread-swap branch from b59a906 to 924935d Compare April 28, 2026 22:16
Copy link
Copy Markdown
Contributor

@godlygeek godlygeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@godlygeek godlygeek enabled auto-merge (rebase) April 28, 2026 22:43
@godlygeek godlygeek merged commit faf7d12 into bloomberg:main Apr 28, 2026
17 of 18 checks passed
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.

Crash on gevent: AttributeError: type object 'Thread' has no attribute '_name'

3 participants