Skip to content

Commit 31f9021

Browse files
Fix terminal persistence bug on server restart
Two bugs caused terminals to be incorrectly marked as "exited" on restart while their dtach sessions were still running: 1. CLI spawned server without VIBORA_DIR, so it inherited CWD and could resolve wrong .vibora directory if run from a folder with one 2. Race condition in onExit handler during graceful shutdown - when detach() killed the PTY, the handler could mark terminals as exited Also fix unused cache variables in monitoring.ts - the cache was being written but never read.
1 parent 642cead commit 31f9021

4 files changed

Lines changed: 25 additions & 1 deletion

File tree

cli/src/commands/up.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { output } from '../utils/output'
66
import { CliError, ExitCodes } from '../utils/errors'
77
import { writePid, readPid, removePid, isProcessRunning, getPort } from '../utils/process'
88
import { confirm } from '../utils/prompt'
9+
import { getViboraDir } from '../utils/server'
910
import {
1011
isDtachInstalled,
1112
isBunInstalled,
@@ -134,6 +135,9 @@ export async function handleUpCommand(flags: Record<string, string>) {
134135
const ptyLibPath = join(packageRoot, 'lib', ptyLibName)
135136

136137
// Start the bundled server
138+
// Explicitly set VIBORA_DIR to ensure consistent path resolution
139+
// regardless of where the CLI was invoked from
140+
const viboraDir = getViboraDir()
137141
console.error('Starting Vibora server...')
138142
const serverProc = spawn('bun', [serverPath], {
139143
detached: true,
@@ -142,6 +146,7 @@ export async function handleUpCommand(flags: Record<string, string>) {
142146
...process.env,
143147
NODE_ENV: 'production',
144148
PORT: port.toString(),
149+
VIBORA_DIR: viboraDir,
145150
VIBORA_PACKAGE_ROOT: packageRoot,
146151
BUN_PTY_LIB: ptyLibPath,
147152
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vibora",
33
"private": true,
4-
"version": "1.12.2",
4+
"version": "1.12.3",
55
"description": "The Vibe Engineer's Cockpit",
66
"license": "PolyForm-Shield-1.0.0",
77
"type": "module",

server/routes/monitoring.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,11 @@ async function fetchClaudeUsage(token: string): Promise<ClaudeUsageResponse> {
908908
monitoringRoutes.get('/claude-usage', async (c) => {
909909
const now = Date.now()
910910

911+
// Return cached result if still fresh
912+
if (cachedUsage && now - usageCacheTimestamp < USAGE_CACHE_MS) {
913+
return c.json(cachedUsage)
914+
}
915+
911916
// Check if z.ai is enabled
912917
const zaiSettings = getZAiSettings()
913918

server/terminal/terminal-session.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export class TerminalSession {
6666
private onData: (data: string) => void
6767
private onExit: (exitCode: number) => void
6868

69+
// Flag to indicate we're intentionally detaching (not exiting)
70+
// Prevents race condition where onExit marks terminal as exited during graceful detach
71+
private isDetaching = false
72+
6973
// Tab association
7074
private _tabId?: string
7175
private _positionInTab: number
@@ -191,6 +195,12 @@ export class TerminalSession {
191195

192196
this.pty.onExit(({ exitCode }) => {
193197
this.pty = null
198+
199+
// If we're intentionally detaching, don't mark as exited
200+
if (this.isDetaching) {
201+
return
202+
}
203+
194204
const dtach = getDtachService()
195205

196206
if (!dtach.hasSession(this.id)) {
@@ -209,8 +219,12 @@ export class TerminalSession {
209219
this.buffer.saveToDisk()
210220

211221
if (this.pty) {
222+
// Set flag BEFORE killing to prevent onExit from marking as exited
223+
this.isDetaching = true
212224
this.pty.kill()
213225
this.pty = null
226+
// Reset flag after kill completes
227+
this.isDetaching = false
214228
}
215229
}
216230

0 commit comments

Comments
 (0)