-
Notifications
You must be signed in to change notification settings - Fork 541
refactor(internal): use fork-safe locks and threads consistently #19796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
P403n1x87
wants to merge
5
commits into
main
Choose a base branch
from
chore/safe-threading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b07af6
refactor(internal): use fork-safe locks and threads consistently
P403n1x87 d4f0832
add linting rules
P403n1x87 8d74be3
prevent pending double-start
P403n1x87 dbef96b
Merge remote-tracking branch 'origin/main' into chore/safe-threading
P403n1x87 6ee32ae
add thread tests
P403n1x87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| id: threading-lock-usage | ||
| message: Use ddtrace.internal.threads.Lock/RLock instead of threading.Lock/RLock | ||
| severity: error | ||
| language: python | ||
| files: | ||
| - "ddtrace/**" | ||
| ignores: | ||
| - "ddtrace/internal/threads.py" | ||
| - "ddtrace/internal/_unpatched.py" | ||
| - "ddtrace/vendor/**" | ||
| - "ddtrace/appsec/_iast/_taint_tracking/_vendor/**" | ||
| - "ddtrace/testing/**" | ||
| - "ddtrace/internal/openfeature/**" | ||
| - "ddtrace/contrib/internal/pytorch/**" | ||
| - "ddtrace/llmobs/_integrations/_bedrock_inference_profiles.py" | ||
| - "ddtrace/appsec/_iast/_overhead_control_engine.py" | ||
| - "ddtrace/profiling/_faulthandler.py" | ||
| rule: | ||
| any: | ||
| - pattern: threading.Lock() | ||
| - pattern: threading.RLock() | ||
| - pattern: from threading import Lock | ||
| - pattern: from threading import RLock | ||
| - pattern: from threading import Lock as $ALIAS | ||
| - pattern: from threading import RLock as $ALIAS | ||
| - pattern: from threading import $$$IMPORTS, Lock | ||
| - pattern: from threading import $$$IMPORTS, Lock as $ALIAS | ||
| - pattern: from threading import Lock, $$$IMPORTS | ||
| - pattern: from threading import Lock as $ALIAS, $$$IMPORTS | ||
| - pattern: from threading import $$$IMPORTS, RLock | ||
| - pattern: from threading import $$$IMPORTS, RLock as $ALIAS | ||
| - pattern: from threading import RLock, $$$IMPORTS | ||
| - pattern: from threading import RLock as $ALIAS, $$$IMPORTS | ||
| note: | | ||
| gevent's monkey-patching replaces `threading.Lock`/`threading.RLock` with | ||
| green (cooperative) equivalents. Mixing those with dd-trace-py's real OS | ||
| threads causes deadlocks, since a green lock held by a real thread can never | ||
| be released by a greenlet waiting on the event loop. | ||
|
|
||
| `ddtrace.internal.threads.Lock`/`RLock` grab a reference to the real, | ||
| unpatched primitives before any monkey-patching library can run, and are | ||
| fork-safe. Use them instead: | ||
|
|
||
| Before: | ||
| import threading | ||
| lock = threading.Lock() | ||
|
|
||
| After: | ||
| from ddtrace.internal.threads import Lock | ||
| lock = Lock() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| id: threading-lock-usage | ||
| snapshots: | ||
| from threading import Lock as TLock: | ||
| labels: | ||
| - source: from threading import Lock as TLock | ||
| style: primary | ||
| start: 0 | ||
| end: 35 | ||
| from threading import Lock, Thread: | ||
| labels: | ||
| - source: from threading import Lock, Thread | ||
| style: primary | ||
| start: 0 | ||
| end: 34 | ||
| from threading import RLock as TRLock: | ||
| labels: | ||
| - source: from threading import RLock as TRLock | ||
| style: primary | ||
| start: 0 | ||
| end: 37 | ||
| from threading import RLock, Thread: | ||
| labels: | ||
| - source: from threading import RLock, Thread | ||
| style: primary | ||
| start: 0 | ||
| end: 35 | ||
| from threading import Thread, Lock: | ||
| labels: | ||
| - source: from threading import Thread, Lock | ||
| style: primary | ||
| start: 0 | ||
| end: 34 | ||
| from threading import Thread, RLock: | ||
| labels: | ||
| - source: from threading import Thread, RLock | ||
| style: primary | ||
| start: 0 | ||
| end: 35 | ||
| import threading; lock = threading.Lock(): | ||
| labels: | ||
| - source: threading.Lock() | ||
| style: primary | ||
| start: 25 | ||
| end: 41 | ||
| import threading; lock = threading.RLock(): | ||
| labels: | ||
| - source: threading.RLock() | ||
| style: primary | ||
| start: 25 | ||
| end: 42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| id: threading-lock-usage | ||
| valid: | ||
| # These should NOT trigger the rule (valid code) | ||
| - from ddtrace.internal.threads import Lock; lock = Lock() | ||
| - from ddtrace.internal.threads import RLock; lock = RLock() | ||
| - import threading; threading.Event() | ||
| - import threading; threading.current_thread() | ||
| - from threading import Thread | ||
|
|
||
| invalid: | ||
| # Direct attribute calls | ||
| - import threading; lock = threading.Lock() | ||
| - import threading; lock = threading.RLock() | ||
| # Aliased imports | ||
| - from threading import Lock as TLock | ||
| - from threading import RLock as TRLock | ||
| # Comma-separated imports | ||
| - from threading import Thread, Lock | ||
| - from threading import Lock, Thread | ||
| - from threading import Thread, RLock | ||
| - from threading import RLock, Thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.