Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5be8761
test: reproduce Claude close teardown leak
austinywang Aug 7, 2026
85db658
fix: unblock Claude teardown when tabs close
austinywang Aug 7, 2026
6163126
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 7, 2026
5d21acb
test: reproduce stranded terminal close teardown
austinywang Aug 7, 2026
cebfba4
fix: isolate blocked terminal close teardowns
austinywang Aug 7, 2026
ef9c634
fix: respect Claude SessionEnd budget overrides
austinywang Aug 7, 2026
aedc1f7
fix: clear inherited Swift warning regressions
austinywang Aug 7, 2026
0e4bb1c
ci: canonicalize app-host config evidence paths
austinywang Aug 7, 2026
f998d9d
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 7, 2026
b1cd4d8
test: cover teardown beyond blocked close slots
austinywang Aug 7, 2026
aee1f97
fix: start process teardown before free admission
austinywang Aug 7, 2026
2de6814
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 7, 2026
b962644
test: cover source-aware network command fixtures
austinywang Aug 7, 2026
e50e33d
fix: make network fixture masking source-aware
austinywang Aug 7, 2026
db465d9
build: pin GhosttyKit for bounded teardown
austinywang Aug 7, 2026
16b499f
revert: drop incomplete determinism lexer change
austinywang Aug 7, 2026
586a39b
fix: reap process-group descendants after child exit
austinywang Aug 7, 2026
46fa5c0
build: pin GhosttyKit for descendant teardown
austinywang Aug 7, 2026
35dbb8c
test: cover teardown racing native surface reads
austinywang Aug 7, 2026
3e05822
test: unblock native access regression execution
austinywang Aug 7, 2026
c9b543f
fix: gate native reads before surface teardown
austinywang Aug 7, 2026
be48e85
build: pin GhosttyKit for stable process groups
austinywang Aug 7, 2026
d31933e
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 7, 2026
6677811
refactor: tighten native access gate ownership
austinywang Aug 7, 2026
f4e112f
refactor: make native access gates runtime-owned
austinywang Aug 7, 2026
8fc0ece
test: cover one-shot native teardown admission
austinywang Aug 7, 2026
230a8e8
build: pin stable process-group GhosttyKit
austinywang Aug 7, 2026
db12524
fix: expose native borrow release within package
austinywang Aug 7, 2026
4db0fa3
fix: gate sharing conformance for Xcode 16
austinywang Aug 7, 2026
4f4b389
fix: keep native reads off teardown actor
austinywang Aug 7, 2026
c54d9d5
build: pin foreground process-group GhosttyKit
austinywang Aug 8, 2026
476f272
Merge branch 'main' of https://github.com/manaflow-ai/cmux into issue…
austinywang Aug 8, 2026
0810cc8
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 8, 2026
22a37de
Merge branch 'main' of https://github.com/manaflow-ai/cmux into issue…
austinywang Aug 9, 2026
1d2334a
Update Ghostty to main-integrated process reaping
austinywang Aug 9, 2026
bd266e2
Pin merged GhosttyKit archive
austinywang Aug 9, 2026
a348c70
Merge branch 'main' of https://github.com/manaflow-ai/cmux into issue…
austinywang Aug 9, 2026
52b914b
test: reproduce concurrent screen-tail admission
austinywang Aug 9, 2026
35b87c0
fix: serialize native screen-tail reads
austinywang Aug 9, 2026
c3d4e12
Update Ghostty process-group reuse safety
austinywang Aug 9, 2026
cb95bbb
test: keep queued reads from delaying teardown
austinywang Aug 9, 2026
0c6f524
fix: acquire native borrow after read admission
austinywang Aug 9, 2026
01720c7
build: pin process-group safety GhosttyKit
austinywang Aug 9, 2026
742d82e
ci: restore app-host validation from main
austinywang Aug 9, 2026
3fff7e4
Merge origin/main into issue-9573-claude-process-leak
austinywang Aug 9, 2026
81911f4
Merge origin/main into issue-9573-claude-process-leak
austinywang Aug 10, 2026
eace2af
Merge origin/main into issue-9573-claude-process-leak
austinywang Aug 10, 2026
deee27d
fix: pair terminal native teardown operations
austinywang Aug 10, 2026
75d6a0c
Merge remote-tracking branch 'origin/main' into issue-9573-claude-pro…
austinywang Aug 10, 2026
9ac0ef5
test: cover native read cleanup and hook timeout
austinywang Aug 10, 2026
f1335e6
test: count every native text free invocation
austinywang Aug 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,18 @@ extension TerminalSurface {
}
#endif

Task { @MainActor in
// Keep free behavior aligned with deinit: perform the runtime teardown on
// the next main-actor turn so SIGHUP delivery is deterministic but non-reentrant.
ghostty_surface_free(surfaceToFree)
callbackContext?.release()
manualIOContext?.release()
teeLease?.release()
}
// Native free can wait for the child process's SessionEnd callback to
// call back into cmux. Keep that join off the main actor; the coordinator
// releases all callback userdata only after the free returns.
runtimeTeardown.enqueueRuntimeTeardown(
id: id,
workspaceId: tabId,
reason: "teardown",
surface: surfaceToFree,
callbackContext: callbackContext,
manualIOContext: manualIOContext,
byteTeeLease: teeLease
)
}

/// Frees the runtime surface while keeping the model alive for an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,65 @@ private final class LifetimeRecordingByteTeeLease: TerminalByteTeeLease, @unchec
#expect(await Set(recorder.freed) == Set(surfaces.map { UInt(bitPattern: $0) }))
}

@Test func stuckCloseFreeDoesNotStrandLaterCloses() async throws {
let coordinator = TerminalSurfaceRuntimeTeardownCoordinator()
let surfaces = (0..<3).map { _ in
UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8)
}
defer { for surface in surfaces { surface.deallocate() } }
let stuckFreeStarted = AsyncStream<Void>.makeStream()
let releaseStuckFree = DispatchSemaphore(value: 0)
let freedSurfaceBits = OSAllocatedUnfairLock(initialState: Set<UInt>())
defer {
releaseStuckFree.signal()
stuckFreeStarted.continuation.finish()
}

let stuckTicket = coordinator.enqueueRuntimeTeardown(
id: UUID(),
workspaceId: UUID(),
reason: "test.stuckClose",
surface: surfaces[0],
callbackContext: nil,
freeSurface: { _ in
stuckFreeStarted.continuation.yield()
_ = releaseStuckFree.wait(timeout: .distantFuture)
}
)
var stuckFreeIterator = stuckFreeStarted.stream.makeAsyncIterator()
_ = await stuckFreeIterator.next()

let laterTickets = surfaces.dropFirst().map { surface in
coordinator.enqueueRuntimeTeardown(
id: UUID(),
workspaceId: UUID(),
reason: "test.laterClose",
surface: surface,
callbackContext: nil,
freeSurface: { pointer in
freedSurfaceBits.withLock {
_ = $0.insert(UInt(bitPattern: pointer))
}
}
)
}

for ticket in laterTickets {
try #require(
await ticket.wait(timeout: .seconds(5)),
"a stuck native free stranded a later close"
)
}
#expect(await stuckTicket.wait(timeout: .zero) == false)
#expect(
freedSurfaceBits.withLock { $0 } ==
Set(surfaces.dropFirst().map { UInt(bitPattern: $0) })
)

releaseStuckFree.signal()
#expect(await stuckTicket.wait(timeout: .seconds(5)))
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

@Test func stuckHibernationFreeDoesNotStrandAnotherAdmissionOrClose() async throws {
let coordinator = TerminalSurfaceRuntimeTeardownCoordinator()
let isolatedSurface = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AppKit
import CmuxTerminalCore
import Foundation
import GhosttyKit
import GhosttyRuntimeTestStubs
import Testing
@testable import CmuxTerminal

Expand All @@ -27,6 +28,42 @@ import Testing
#expect(await wait.value == false)
}

@Test func teardownSurfaceKeepsMainActorResponsiveWhileNativeFreeIsBlocked() async {
let surface = makeSurface()
let runtimeSurface = fakeRuntimeSurface()
surface.installRuntimeSurfaceForTesting(runtimeSurface)
cmux_test_ghostty_surface_free_blocking_begin(runtimeSurface)
defer {
cmux_test_ghostty_surface_free_release()
cmux_test_ghostty_surface_free_blocking_reset()
}

let probeResult = AsyncStream<Bool>.makeStream()
DispatchQueue.global(qos: .userInitiated).async {
guard cmux_test_ghostty_surface_free_wait_until_started() else {
probeResult.continuation.yield(false)
probeResult.continuation.finish()
return
}

Task { @MainActor in
let nativeFreeIsStillBlocked =
cmux_test_ghostty_surface_free_blocking_is_active()
cmux_test_ghostty_surface_free_release()
probeResult.continuation.yield(nativeFreeIsStillBlocked)
probeResult.continuation.finish()
}
}

surface.teardownSurface()

var probeResultIterator = probeResult.stream.makeAsyncIterator()
#expect(
await probeResultIterator.next() == true,
"explicit teardown did not start native free while keeping the main actor responsive"
)
}

@Test func teardownSurfaceKeepsTeeLeaseUntilNativeFree() async {
let recorder = TeardownOrderRecorder()
let surface = makeSurface()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "include/GhosttyRuntimeTestStubs.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>

typedef struct {
uint8_t r;
Expand Down Expand Up @@ -35,6 +37,19 @@ static bool cmux_test_font_binding_succeeds = true;
static void* cmux_test_font_callback_surface = NULL;
static ghostty_font_size_action_cb cmux_test_font_callback = NULL;
static void* cmux_test_font_callback_userdata = NULL;
static pthread_mutex_t cmux_test_surface_free_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cmux_test_surface_free_condition = PTHREAD_COND_INITIALIZER;
static bool cmux_test_surface_free_should_block = false;
static bool cmux_test_surface_free_started = false;
static bool cmux_test_surface_free_released = false;
static void* cmux_test_surface_free_target = NULL;

static struct timespec cmux_test_surface_free_timeout(void) {
return (struct timespec) {
.tv_sec = 5,
.tv_nsec = 0,
};
}

void cmux_test_ghostty_runtime_stubs_reset(void) {
cmux_test_needs_confirm_quit = false;
Expand All @@ -43,6 +58,59 @@ void cmux_test_ghostty_runtime_stubs_reset(void) {
cmux_test_tty_name_call_count = 0;
}

void cmux_test_ghostty_surface_free_blocking_begin(void *surface) {
pthread_mutex_lock(&cmux_test_surface_free_mutex);
cmux_test_surface_free_should_block = true;
cmux_test_surface_free_started = false;
cmux_test_surface_free_released = false;
cmux_test_surface_free_target = surface;
pthread_mutex_unlock(&cmux_test_surface_free_mutex);
}

bool cmux_test_ghostty_surface_free_wait_until_started(void) {
const struct timespec timeout = cmux_test_surface_free_timeout();
pthread_mutex_lock(&cmux_test_surface_free_mutex);
while (!cmux_test_surface_free_started) {
const int result = pthread_cond_timedwait_relative_np(
&cmux_test_surface_free_condition,
&cmux_test_surface_free_mutex,
&timeout
);
if (result != 0) break;
}
const bool started = cmux_test_surface_free_started;
pthread_mutex_unlock(&cmux_test_surface_free_mutex);
return started;
}

bool cmux_test_ghostty_surface_free_blocking_is_active(void) {
pthread_mutex_lock(&cmux_test_surface_free_mutex);
const bool active =
cmux_test_surface_free_should_block
&& cmux_test_surface_free_started
&& !cmux_test_surface_free_released
&& cmux_test_surface_free_target != NULL;
pthread_mutex_unlock(&cmux_test_surface_free_mutex);
return active;
}

void cmux_test_ghostty_surface_free_release(void) {
pthread_mutex_lock(&cmux_test_surface_free_mutex);
cmux_test_surface_free_released = true;
pthread_cond_broadcast(&cmux_test_surface_free_condition);
pthread_mutex_unlock(&cmux_test_surface_free_mutex);
}

void cmux_test_ghostty_surface_free_blocking_reset(void) {
pthread_mutex_lock(&cmux_test_surface_free_mutex);
cmux_test_surface_free_should_block = false;
cmux_test_surface_free_started = false;
cmux_test_surface_free_released = true;
cmux_test_surface_free_target = NULL;
pthread_cond_broadcast(&cmux_test_surface_free_condition);
pthread_mutex_unlock(&cmux_test_surface_free_mutex);
}

void cmux_test_ghostty_renderer_realized_begin(void* surface) {
cmux_test_renderer_realized_target = surface;
cmux_test_renderer_realized_call_count = 0;
Expand Down Expand Up @@ -231,6 +299,25 @@ bool ghostty_surface_set_font_size_action_callback(

void ghostty_surface_config_new(void) {}
void ghostty_surface_free(void *surface) {
const struct timespec timeout = cmux_test_surface_free_timeout();
pthread_mutex_lock(&cmux_test_surface_free_mutex);
if (cmux_test_surface_free_should_block
&& surface == cmux_test_surface_free_target) {
cmux_test_surface_free_started = true;
pthread_cond_broadcast(&cmux_test_surface_free_condition);
while (!cmux_test_surface_free_released) {
const int result = pthread_cond_timedwait_relative_np(
&cmux_test_surface_free_condition,
&cmux_test_surface_free_mutex,
&timeout
);
if (result != 0) break;
}
cmux_test_surface_free_should_block = false;
cmux_test_surface_free_target = NULL;
}
pthread_mutex_unlock(&cmux_test_surface_free_mutex);

if (cmux_test_font_callback_surface == surface) {
cmux_test_font_callback_surface = NULL;
cmux_test_font_callback = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ ghostty_string_s ghostty_surface_tty_name(void *surface);

void cmux_test_ghostty_runtime_stubs_reset(void);
void cmux_test_ghostty_runtime_stubs_set_close_state(bool needs_confirm, uint64_t foreground_pid, const char* tty_name);
void cmux_test_ghostty_surface_free_blocking_begin(void *surface);
bool cmux_test_ghostty_surface_free_wait_until_started(void);
bool cmux_test_ghostty_surface_free_blocking_is_active(void);
void cmux_test_ghostty_surface_free_release(void);
void cmux_test_ghostty_surface_free_blocking_reset(void);
uint32_t cmux_test_ghostty_tty_name_call_count(void);
void cmux_test_ghostty_renderer_realized_begin(void *surface);
void cmux_test_ghostty_renderer_realized_reset(void);
Expand Down
31 changes: 29 additions & 2 deletions Resources/bin/cmux-claude-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,31 @@ install_cmux_node_options() {
fi
}

# Claude gives SessionEnd a separate exit budget and caps each hook's configured
# timeout to that budget. An absent or zero override makes Claude derive the
# budget from the configured hooks, so only raise an inherited positive value
# that would cut cmux's 10-second callback short.
ensure_claude_session_end_hook_timeout_budget() {
local minimum_timeout_ms=10000
[[ ${CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS+x} ]] || return

local configured_timeout_ms="$CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS"

if ! cmux_claude_wrapper_is_nonnegative_integer "$configured_timeout_ms"; then
unset CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS
return
fi

# Compare digit counts instead of evaluating user input as shell arithmetic:
# environment values can exceed Bash's integer range. The minimum is a power
# of ten, so every positive value with fewer significant digits is smaller.
local significant_digits="${configured_timeout_ms#"${configured_timeout_ms%%[!0]*}"}"
if [[ -n "$significant_digits" \
&& ${#significant_digits} -lt ${#minimum_timeout_ms} ]]; then
export CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS="$minimum_timeout_ms"
fi
}

encode_launch_argv() {
{
printf '%s\0' "$REAL_CLAUDE"
Expand Down Expand Up @@ -906,14 +931,16 @@ export CMUX_AGENT_LAUNCH_KIND="claude"
export CMUX_AGENT_LAUNCH_EXECUTABLE="$REAL_CLAUDE"
export CMUX_AGENT_LAUNCH_ARGV_B64="$(encode_launch_argv "$@")"
export CMUX_AGENT_LAUNCH_CWD="$PWD"
ensure_claude_session_end_hook_timeout_budget
install_cmux_node_options

# Build Claude settings JSON.
# Claude Code merges --settings additively with the user's own settings.json.
# - preferredNotifChannel: disables Claude Code's OSC/terminal notification
# channel inside cmux so hooks are the only notification source.
# - SessionStart/Stop/Notification: existing lifecycle hooks
# - SessionEnd: cleanup when Claude exits (covers Ctrl+C where Stop doesn't fire)
# - SessionEnd: cleanup when Claude exits (covers Ctrl+C where Stop doesn't
# fire). SYNC with a 10s timeout and matching dedicated Claude exit budget.
# - UserPromptSubmit: clears "Needs input" and sets "Running" on new prompt
# - PreToolUse: rejects unsupported durable cron requests synchronously, then
# (async) clears "Needs input" / sets "Running" for ordinary tools, and flags
Expand All @@ -940,7 +967,7 @@ install_cmux_node_options
# timeout because it may run a summarization subprocess; it gates itself
# on the workspaceAutoNaming setting via a socket probe, so it is a
# no-op when the feature is disabled.
HOOKS_JSON='{"preferredNotifChannel":"notifications_disabled","hooks":{"SessionStart":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude session-start","timeout":10}]}],"Stop":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude stop","timeout":10}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":10,"async":true}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude auto-name","timeout":120,"async":true}]}],"SubagentStop":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":10,"async":true}]}],"SessionEnd":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude session-end","timeout":1}]}],"Notification":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude notification","timeout":10}]}],"UserPromptSubmit":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude prompt-submit","timeout":10}]}],"PreToolUse":[{"matcher":"CronCreate","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude cron-create-guard","timeout":5}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude pre-tool-use","timeout":5,"async":true}]}],"PostToolUse":[{"matcher":"PushNotification","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude push-notification","timeout":10,"async":true}]}],"PermissionRequest":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":125}]}]}}'
HOOKS_JSON='{"preferredNotifChannel":"notifications_disabled","hooks":{"SessionStart":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude session-start","timeout":10}]}],"Stop":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude stop","timeout":10}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":10,"async":true}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude auto-name","timeout":120,"async":true}]}],"SubagentStop":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":10,"async":true}]}],"SessionEnd":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude session-end","timeout":10}]}],"Notification":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude notification","timeout":10}]}],"UserPromptSubmit":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude prompt-submit","timeout":10}]}],"PreToolUse":[{"matcher":"CronCreate","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude cron-create-guard","timeout":5}]},{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude pre-tool-use","timeout":5,"async":true}]}],"PostToolUse":[{"matcher":"PushNotification","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks claude push-notification","timeout":10,"async":true}]}],"PermissionRequest":[{"matcher":"","hooks":[{"type":"command","command":"\"${CMUX_CLAUDE_HOOK_CMUX_BIN:-cmux}\" hooks feed --source claude","timeout":125}]}]}}'

# Fold any user-provided --settings into HOOKS_JSON before launching.
#
Expand Down
Loading
Loading