Skip to content

Implement disableable thread pool depth check to prevent synchronous promise deadlocks #87

@coderabbitai

Description

@coderabbitai

Summary

When synchronous promises are deeply nested, the call chain can exhaust the ThreadPoolExecutor (currently max_workers=128 in Defaults.PROMISING_THREAD_POOL). The deepest synchronous function waits for a child promise that can never be scheduled because all worker threads are already occupied by the waiting chain — resulting in a deadlock.

Proposed solution

Track synchronous call depth along the code path that schedules work into the ThreadPoolExecutor and raises a clear, actionable error (e.g., ThreadPoolDepthExceededError or similar) when depth >= executor.max_workers, instead of deadlocking silently.

The check should be disableable via a toggle (e.g., a flag on Defaults, an env-var like PROMISING_DISABLE_DEPTH_CHECK, or an optional parameter on Promise/PromisingFunction) so callers who know their workload is safe can opt out.

Acceptance criteria

  • Synchronous call depth is tracked at the scheduling boundary.
  • A clear exception is raised when depth reaches max_workers.
  • The check is disabled by a configurable toggle (default: enabled).
  • Unit tests assert the exception fires at the depth limit.
  • Unit tests assert the toggle suppresses the exception.

References

/cc @teremterem

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions