Repository navigation
Scoping a Server no longer kills it - #728
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #728 +/- ##
==========================================
- Coverage 57.72% 57.71% -0.02%
==========================================
Files 29 29
Lines 4360 4363 +3
Branches 882 882
==========================================
+ Hits 2517 2518 +1
- Misses 1533 1535 +2
Partials 310 310 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
846fb7c to
7ea0444
Compare
Code reviewFound 1 issue:
libtmux/docs/topics/context_managers.md Lines 117 to 141 in 7ea0444 "Nested context managers" still says the block gives you "every layer torn down for you", and item 4 reads "The server is killed when exiting its context". The libtmux/docs/topics/context_managers.md Lines 142 to 149 in 7ea0444 "Benefits" then says "you never manually call the {meth} The doctests cannot catch this: the nested block asserts no output, so the prose can drift from the behaviour silently. Lines 145 to 150 in 7ea0444 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
60c8056 to
9114092
Compare
4d60f41 to
9befff4
Compare
5eeb4f4 to
a3e9d41
Compare
412efa7 to
0197be4
Compare
why: A Server is a handle on a socket, not a connection -- the same handle addresses a daemon whether or not this process started it, and a bare Server() addresses whichever tmux the caller has open. Scoping one to a block was destroying it, along with every session in it. Inferring ownership from is_alive() was considered and rejected. That predicate folds every way of failing to reach a server into False by contract, so a transient tmux error at __enter__ would have read as "this block started it" and killed a daemon that predated the block -- the same bug through a different door. Fixes #724. what: - Default kill_on_exit to False; __exit__ kills only when asked - __enter__ returns self and probes nothing - Say in the Server docstring that a server, unlike a session, window or pane, does not tear itself down, and why - Rework docs/topics/context_managers.md, which promised unconditional teardown for all four classes - Test the default, the opt-in, and a bare Server() on the default socket -- the shape the report was actually about, which the TestServer factory cannot reproduce
why: The page promised unconditional teardown for all four classes, so a reader following it would expect `with Server()` to kill a server it did not start. what: - Say that a `Server` context leaves the daemon running unless it was built with `kill_on_exit=True`, and show both. - Correct the nested-context walkthrough, whose summary still listed the server among the layers torn down.
why: Scope exit must preserve the daemon and programs selected by a borrowed handle, including on exceptions and concurrent creation. what: - Check daemon and program identity across normal and exceptional exit - Cover a daemon started by another handle during the scope - Isolate the default-socket regression and always clean its server - Document explicit teardown migration and child context ownership
why: Leaving a with Server() block no longer kills the server, which changes what existing code tears down. what: - Add a Breaking changes entry for the new default and kill_on_exit, with the migration
0197be4 to
e479ae3
Compare
…ample Set versionadded and versionchanged directives on Server.kill_on_exit and Server.__exit__ to 0.69, broaden the MIGRATION baseline to pre-0.69.0 releases, pass kill_on_exit=True in the nested teardown doctest, and link exit-empty on man.openbsd.org.
why: Pick up libtmux 0.69.0. Server context managers leave a running tmux server alive on exit unless kill_on_exit=True is set, @ user options stay strings in show_option() and show_options(), and empty tmux format fields hydrate as "" instead of None while unset fields stay None. tmuxp requires no source changes for the upgrade. what: - Bump libtmux ~=0.68.0 -> ~=0.69.0 in pyproject.toml - Update uv.lock to libtmux 0.69.0 from PyPI (only libtmux moves) libtmux 0.69.0 (2026-11-27): - Server.__exit__() preserves running servers unless kill_on_exit=True is set (tmux-python/libtmux#728) - @ user options stay strings in show_option() and show_options() (tmux-python/libtmux#826) - Empty tmux format fields hydrate as "" while unset fields stay None (tmux-python/libtmux#900) See also: - https://github.com/tmux-python/libtmux/releases/tag/v0.69.0 - https://libtmux.git-pull.com/history/#libtmux-0-69-0-2026-11-27
Summary
with Server() as server:.kill_on_exit: bool = FalseonServerto opt into daemon teardown on exit.Serverdiffers fromSession,Window, andPaneinServer,docs/topics/context_managers.md,CHANGES, andMIGRATION.Fixes #724
Why scoping is not ownership
A
Serveris a handle on a socket, not a connection. The same handle addresses a daemon whether or not this process started it, and a bareServer()addresses the default socket — usually the tmux the reader has been working in all day. Entering awithblock scopes the handle without granting permission to destroy a shared daemon.Session,Window, andPaneretain their existing context-manager behavior: leaving the block destroys the selected object, including one retrieved from an existing server.Normal and exceptional exits preserve the selected server, including a daemon started by another client during the block. The native
kill-servercommand destroys every session on that server. tmux may still exit after its last session ends according toexit-empty.Before / After
Before, leaving an empty
withblock killed the server (is_alive()returnedFalse) and destroyed every session on that socket:After, leaving the block preserves the server (
is_alive()returnsTrue):Opt into teardown explicitly with
kill_on_exit=True:Design decisions
__enter__cannot infer creation ownership:Server.is_alive()returnsFalseon any transient error reaching the socket, so inferring ownership fromnot self.is_alive()at entry would misclassify an existing server and kill it on exit, while a socket itself carries no provenance of who started the daemon.subprocess.Popen.__exit__andsqlite3.Connection.__exit__, where scoping does not destroy the underlying process or connection.kill_on_exitis a plainbooldefaulting toFalse, matching action-named flags such asPane.display_popup(close_on_exit=...)and tmux'sremain-on-exit/exit-emptyoptions rather than claiming unverified ownership (owns_server).temp_serverremains the doc anchor for theTestServerpytest fixture; isolated temporary-server helpers remain tracked in AddServer.private()context manager #872.Test plan
test_server_context_managerverifies the default preserves a running server, including one started inside the blocktest_server_context_manager_spares_borrowed_serververifies daemon PID, session ID, and pane process survival across normal and raising exits for pre-existing and mid-block serverstest_server_context_manager_spares_the_default_socketverifies bareServer()under$TMUX_TMPDIRtest_server_context_manager_kill_on_exit_trueandtest_server_context_manager_kill_on_exit_true_when_block_started_itverify opt-in teardownServer,docs/topics/context_managers.md, andMIGRATION, plusruff,mypy, andsphinx-build -WFollow-up, not in this PR
Session.from_session_id,Window.from_window_id, andPane.from_pane_idstill kill their target when used inwith; creation-ownership tracking across child scopes remains tracked in #804 and #925.