Skip to content

Commit 521e045

Browse files
Keep synchronous window title errors with their caller
1 parent e72ceb5 commit 521e045

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎cmd2/cmd2.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,9 @@ def set_window_title(self, title: str) -> None:
59595959
self._window_title_callback_loop = loop
59605960
return
59615961

5962-
self._flush_pending_window_title()
5962+
# Keep ownership until the direct write finishes, so another caller or prompt cleanup cannot consume
5963+
# this request and receive or suppress its output error.
5964+
self._flush_pending_window_title()
59635965

59645966
def _get_prompt_loop(self) -> asyncio.AbstractEventLoop | None:
59655967
"""Return the event loop of the prompt-toolkit application drawing on this terminal, if one is running.

‎tests/test_window_title.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,66 @@ def test_title_output_errors_reach_the_caller_without_a_prompt(monkeypatch, oper
307307
assert app._pending_window_title is None
308308

309309

310+
@pytest.mark.parametrize("closed_loop", [False, True])
311+
@pytest.mark.parametrize("error_type", [OSError, ValueError])
312+
@pytest.mark.parametrize("operation", ["set_title", "flush"])
313+
def test_prompt_cleanup_cannot_swallow_a_direct_title_error(monkeypatch, closed_loop, error_type, operation):
314+
output = title_output(TtyStream())
315+
flush_entered = threading.Event()
316+
release_flush = threading.Event()
317+
caller_threads = []
318+
failed_threads = []
319+
320+
with create_pipe_input() as pipe_input:
321+
app = make_app(monkeypatch, pipe_input, output)
322+
if closed_loop:
323+
loop = asyncio.new_event_loop()
324+
loop.close()
325+
monkeypatch.setattr(app.main_session.app, "loop", loop)
326+
flush = app._flush_pending_window_title
327+
328+
def pause_direct_flush():
329+
if threading.get_ident() == caller_threads[0]:
330+
flush_entered.set()
331+
assert release_flush.wait(timeout=5)
332+
flush()
333+
334+
def fail_output(*_args):
335+
failed_threads.append(threading.get_ident())
336+
raise error_type("terminal closed")
337+
338+
def update_title():
339+
caller_threads.append(threading.get_ident())
340+
app.set_window_title("Fail")
341+
342+
monkeypatch.setattr(app, "_flush_pending_window_title", pause_direct_flush)
343+
monkeypatch.setattr(output, operation, fail_output)
344+
caller = start_worker(update_title)
345+
cleanup = None
346+
try:
347+
assert flush_entered.wait(timeout=5)
348+
cleanup = start_worker(app._flush_pending_window_title_quietly)
349+
# If the caller released ownership before flushing, let cleanup consume the title first.
350+
# Otherwise cleanup must wait for the caller; release it without waiting for a timeout.
351+
if app._window_title_lock.acquire(blocking=False):
352+
app._window_title_lock.release()
353+
cleanup.result(timeout=5)
354+
finally:
355+
release_flush.set()
356+
357+
try:
358+
with pytest.raises(error_type, match="terminal closed"):
359+
caller.result(timeout=5)
360+
finally:
361+
if cleanup is not None:
362+
cleanup.result(timeout=5)
363+
app._flush_pending_window_title_quietly()
364+
365+
assert failed_threads == caller_threads
366+
assert app._pending_window_title is None
367+
assert not app._writing_window_title
368+
369+
310370
@pytest.mark.parametrize("operation", ["set_title", "flush"])
311371
@pytest.mark.parametrize("from_worker", [False, True])
312372
def test_title_output_errors_during_a_prompt(monkeypatch, operation, from_worker):

0 commit comments

Comments
 (0)