Skip to content

feat: v0.1.3 concurrency architecture hardening and defect remediation - #17

Merged
hankaihong1 merged 1 commit into
mainfrom
feat/v0.1.3-concurrency-hardening
Sep 17, 2026
Merged

hankaihong1 merged 1 commit into
mainfrom
feat/v0.1.3-concurrency-hardening

Conversation

@hankaihong1

Copy link
Copy Markdown
Owner

Summary of Changes

This release addresses concurrency code smells and architectural bottlenecks across 8 components in multiloop, establishing formal state-machine invariants, thread safety under Python 3.14t (Free-Threaded / No-GIL), and bumping the version to v0.1.3:

  1. AsyncRWMutex Concurrency Data Race Fix (src/multiloop/rwlock.py):
    • Synchronized _reader_depth and _writer_task with self._depth_lock = threading.Lock().
    • Moved _wake_all() calls outside of the depth lock to prevent deadlocks.
  2. Native Synchronous Channel Communication (src/channel.rs & src/multiloop/primitives.py):
    • Replaced busy-spin polling (time.sleep(0.0005)) with native parking_lot::Condvar in RawAsyncChannel.
    • Automatically releases the Python GIL via py.detach() during blocking waits.
    • Added ReceiveChannel.recv_sync(timeout=None).
  3. Dispatcher & Metrics Optimization (src/metrics.rs & src/pool.rs):
    • Added AtomicMetrics.add_global_pull(usize) for single atomic updates during batch pulls.
    • Upgraded NativeWorkerPool.metrics to parking_lot::RwLock.
  4. Cross-Language Exception Hierarchy Standardization (src/lib.rs, src/multiloop/exceptions.py, src/multiloop/pool.py):
    • Exported ThreadPoolClosedError directly from PyO3 Rust extension.
    • Unified Python exception inheritance hierarchy and removed _RustPoolClosedError split.
  5. O(1) Waiter Unregistration & Short-Circuit Removal (src/multiloop/_channel_base.py & src/channel.rs):
    • Enforced $O(1)$ removals in _discard_waiter via OrderedDict.pop().
    • Applied short-circuit pointer comparisons in Rust waiter unregistration.
  6. GC Finalizer Safety Governance (src/multiloop/primitives.py & tests):
    • Eliminated self._wg.done() inside _TrackedCoroutine.__del__ to prevent cross-thread deadlocks under multi-threaded GC.
    • Emits RuntimeWarning when unawaited coroutines leak.
  7. Event-Driven Dispatching & Drain Barrier (src/multiloop/pool.py):
    • Replaced 50ms polling loops in _worker_dispatcher with pure event-driven notify_event.wait().
    • Implemented self._drain_barrier = threading.Condition(self._lock) for graceful shutdown.
  8. Cross-Loop Structured Concurrency (src/multiloop/_taskgroup.py & src/multiloop/context.py):
    • Added pool support to TaskGroup with isolated per-task child CancelScope instances.
    • Implemented remote drain barrier on context exit, bidirectional cascading cancellation, and unified ExceptionGroup aggregation.
    • Unified AsyncContext.task_group(pool=...) lifecycle tracking and automatic cascading cancellation.
  9. Documentation, Type Stubs & Version Bump:
    • Full docstrings and Rustdoc comments updated across all modified files.
    • Synchronized __init__.pyi type stubs.
    • Aligned English and Chinese documentation (docs/API.md ↔ docs/API_ZH.md).
    • Bumped version to v0.1.3.

Verification

  • Rust Core Tests: 23 passed.
  • Python Tests: 341 passed (100%).
  • Documentation consistency tests (tests/test_docs.py): 5 passed.
  • All gates in make lint clean (ruff, mypy strict, pyright, clippy, cargo fmt).

- Fix AsyncRWMutex Free-Threaded data races with _depth_lock and out-of-lock wakeups
- Native Condvar blocking send_sync/recv_sync in RawAsyncChannel and ReceiveChannel
- Optimize NativeWorkerPool scheduling with AtomicMetrics batch additions and RwLock
- Standardize cross-language ThreadPoolClosedError exception hierarchy
- Enforce O(1) waiter unregistration and short-circuit RawAsyncChannel removals
- Eliminate GC finalizer cross-thread re-entrancy in _TrackedCoroutine.__del__
- Event-driven worker dispatching and Condition drain barrier in EventLoopThreadPool
- Cross-loop structured concurrency with TaskGroup(pool=...) and unified AsyncContext
- Full docstrings, type stubs, and dual-language API documentation synchronization
- Bump version to v0.1.3
@hankaihong1
hankaihong1 merged commit babae24 into main Sep 17, 2026
11 checks passed
@hankaihong1
hankaihong1 deleted the feat/v0.1.3-concurrency-hardening branch September 17, 2026 11:12
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.

1 participant