Skip to content

retry: fail loud when deadline_s is used off the main thread#711

Draft
kmontemayor2-sc wants to merge 5 commits into
mainfrom
kmonte/retry-deadline-main-thread-guard
Draft

retry: fail loud when deadline_s is used off the main thread#711
kmontemayor2-sc wants to merge 5 commits into
mainfrom
kmonte/retry-deadline-main-thread-guard

Conversation

@kmontemayor2-sc

@kmontemayor2-sc kmontemayor2-sc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

retry(deadline_s=...) enforces the deadline with a SIGALRM timeout, which CPython only permits in the main thread of the main interpreter. Reached from a worker thread it raised the opaque "signal only works in main thread of the main interpreter" ValueError from signal.signal.

Guard the deadline path: off the main thread, raise a RuntimeError that names the decorated function and points the caller at enforcing the deadline on the operation itself (e.g. a client-native timeout). Main-thread behavior is unchanged. Adds a test for the off-main-thread path.

This keeps the same behavior (failing if we use this var wrong) just provides better error mesasges :)

Scope of work done

Where is the documentation for this feature?: N/A

Did you add automated tests or write a test plan?

Updated Changelog.md? NO

Ready for code review?: NO

kmonte and others added 2 commits July 20, 2026 19:10
retry(deadline_s=...) enforces the deadline with a SIGALRM timeout, which
CPython only permits in the main thread of the main interpreter. Reached from
a worker thread it raised the opaque "signal only works in main thread of the
main interpreter" ValueError from signal.signal.

Guard the deadline path: off the main thread, raise a RuntimeError that names
the decorated function and points the caller at enforcing the deadline on the
operation itself (e.g. a client-native timeout). Main-thread behavior is
unchanged. Adds a test for the off-main-thread path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Follow-up on the previous commit per review: drop the explanatory comment
(the RuntimeError message is self-explanatory), drop the unnecessary
type: ignore[type-var] on the wrapper (ty passes without it), and remove the
unit test. Net change on this branch is just the guard.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread gigl/common/utils/retry.py Outdated
Comment thread gigl/common/utils/retry.py Outdated
kmontemayor2-sc and others added 2 commits July 20, 2026 13:54
Co-authored-by: yliu2-sc <142346463+yliu2-sc@users.noreply.github.com>
@wraps(f)
def f_retry_with_deadline(*args, **kwargs) -> T:
if threading.current_thread() is not threading.main_thread():
raise RuntimeError(

@svij-sc svij-sc Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this reads as an odd RuntimeError.
Is this what we really want vs. somehting more descriptive of what the issue was? i.e. we failed __ after __x tries, n time, in ___ thread/process.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This change isn't about the timeout or retry failing. The code (as it stands) currently fails, and I'm adding a new error message that's hopefully more descriptive. the issue here is that we can't use deadline_s on non-main threads (python only lets you use sigalarm on main thread).

I can re-word this to be more descriptive.

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.

4 participants