0.2.1: release handlers on close to fix a per-connection leak #2

Merged
selimaj-dev merged 1 commits from release-handlers-on-close into master 2026-09-25 03:46:27 +00:00
Owner

Summary

Handlers usually capture a clone of their own Session, for example to remove it from a map in on_close or to call notify. Because the Session owns those handlers, the two kept each other alive forever. A closed session, and everything its handlers captured, was never freed. The socket itself did close, but a small amount of memory leaked per connection. 0.1.x had the same leak.

The fix is in shutdown, which still runs exactly once:

  • the on_close handler is now taken out and run, instead of cloned and run
  • the request and notification handlers are then cleared

Requests still queued after close already failed to send, so behaviour doesn't change.

The version is bumped to 0.2.1.

Testing

  • New test closing_releases_handlers_that_capture_the_session. It registers a request handler and an on_close handler that both capture the session and a sentinel Arc, closes the session, and waits for the sentinel to be released. It fails on master (times out after 5 s) and passes with this change.
  • All 14 tests pass on 10 of 10 runs of cargo test --all-features.
  • Clippy is clean and cargo publish --dry-run passes.

🤖 Generated with Claude Code

## Summary Handlers usually capture a clone of their own `Session`, for example to remove it from a map in `on_close` or to call `notify`. Because the `Session` owns those handlers, the two kept each other alive forever. A closed session, and everything its handlers captured, was never freed. The socket itself did close, but a small amount of memory leaked per connection. 0.1.x had the same leak. The fix is in `shutdown`, which still runs exactly once: - the `on_close` handler is now taken out and run, instead of cloned and run - the request and notification handlers are then cleared Requests still queued after close already failed to send, so behaviour doesn't change. The version is bumped to 0.2.1. ## Testing - New test `closing_releases_handlers_that_capture_the_session`. It registers a request handler and an `on_close` handler that both capture the session and a sentinel `Arc`, closes the session, and waits for the sentinel to be released. It **fails on master** (times out after 5 s) and passes with this change. - All 14 tests pass on 10 of 10 runs of `cargo test --all-features`. - Clippy is clean and `cargo publish --dry-run` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
selimaj-dev added 1 commit 2026-09-25 03:45:11 +00:00
Handlers commonly capture a clone of their own Session, and the Session
owns its handlers, so a closed session was never freed. On shutdown,
take the on_close handler instead of cloning it and clear the request
and notification handlers after it runs.

Bump to 0.2.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
selimaj-dev merged commit bc45bff9a3 into master 2026-09-25 03:46:27 +00:00
selimaj-dev deleted branch release-handlers-on-close 2026-09-25 03:46:30 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: selimaj-dev/session-rs#2