From cd4b7cb16f0fe103e8ad5310763c7bdd169b5d77 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Sun, 18 Oct 2026 23:25:25 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A2=85=EB=A3=8C=20=EC=8B=9C=20'Tray=20is=20d?= =?UTF-8?q?estroyed'=20=EC=98=A4=EB=A5=98=20=EC=B0=BD:=20=ED=8A=B8?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=20=C2=B7=20=EC=83=81=ED=83=9C=20=EA=B0=B1?= =?UTF-8?q?=EC=8B=A0=20=ED=83=80=EC=9D=B4=EB=A8=B8=EB=A5=BC=20=EB=A9=88?= =?UTF-8?q?=EC=B6=94=EA=B3=A0=20=ED=8A=B8=EB=A0=88=EC=9D=B4=EB=A5=BC=20?= =?UTF-8?q?=EB=B9=84=EC=9A=B4=20=EB=92=A4=20=EC=97=86=EC=95=A4=EB=8B=A4=20?= =?UTF-8?q?=C2=B7=20Electron=20=EC=9D=B4=20=EB=A8=BC=EC=A0=80=20=EC=97=86?= =?UTF-8?q?=EC=95=A4=20=ED=8A=B8=EB=A0=88=EC=9D=B4=EB=8F=84=20=EA=B1=B4?= =?UTF-8?q?=EB=93=9C=EB=A6=AC=EC=A7=80=20=EC=95=8A=EC=9D=8C=20=C2=B7=20?= =?UTF-8?q?=EC=A2=85=EB=A3=8C=20=EC=A4=91=20STT=20=EB=8A=94=20=EC=83=88=20?= =?UTF-8?q?=EC=9E=91=EC=97=85=EC=9D=84=20=EA=BA=BC=EB=82=B4=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EA=B3=A0=20=EC=9B=8C=EC=BB=A4=20=EC=A2=85=EB=A3=8C?= =?UTF-8?q?=EB=A1=9C=20=EC=8B=A4=ED=8C=A8=ED=95=9C=20=EC=9E=91=EC=97=85?= =?UTF-8?q?=EC=9D=84=20=EB=8B=AB=ED=9E=8C=20DB=20=EC=97=90=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=8C=20(=EC=8B=9C?= =?UTF-8?q?=ED=97=98=20=EC=B6=94=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- app/src/core/index.ts | 1 + app/src/core/stt/pipeline.ts | 11 ++++++- app/src/main/main.ts | 21 +++++++++----- app/test/stt-shutdown.test.ts | 54 +++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 app/test/stt-shutdown.test.ts diff --git a/app/src/core/index.ts b/app/src/core/index.ts index 9afefaa..81c47cb 100644 --- a/app/src/core/index.ts +++ b/app/src/core/index.ts @@ -189,6 +189,7 @@ export async function startCore(opts: CoreOptions) { relay.stop(); retention.stop(); games.stop(); + stt.stop(); await bot.stop(); await worker.stop(); await api.close(); diff --git a/app/src/core/stt/pipeline.ts b/app/src/core/stt/pipeline.ts index a8ece1e..7a7e927 100644 --- a/app/src/core/stt/pipeline.ts +++ b/app/src/core/stt/pipeline.ts @@ -81,6 +81,8 @@ export class SttPipeline { private queued = new Set(); private busy = false; private processing: string | null = null; + /** 앱 종료 중: 새 작업을 꺼내지 않고, 워커가 끊겨 실패한 작업은 DB(곧 닫힘)에 기록하지 않는다. 남은 발화는 다음 시작 때 다시 처리 */ + private stopped = false; /** 한 발화의 변환이 끝났을 때 (녹음 파일 이름 맞추기) */ onDone: ((id: string) => void) | null = null; /** 예전 녹음 고치기가 끝났을 때 */ @@ -238,8 +240,13 @@ export class SttPipeline { return null; } + /** 종료 시작 (워커 · DB 를 닫기 전에 부른다) */ + stop(): void { + this.stopped = true; + } + private pump(): void { - if (this.busy || !this.worker.ready) return; + if (this.stopped || this.busy || !this.worker.ready) return; const idx = this.queue.findIndex((j) => this.pickSlot(j) !== null); if (idx < 0) return; const job = this.queue.splice(idx, 1)[0]; @@ -254,6 +261,7 @@ export class SttPipeline { this.busy = false; this.processing = null; this.status.processing = null; + if (this.stopped) return; try { if (!this.isBusy(job.id)) this.onDone?.(job.id); } catch (e) { @@ -320,6 +328,7 @@ export class SttPipeline { 5 * 60000, ); } catch (e: any) { + if (this.stopped) return; const attempts = this.store.bumpAttempt(u.id); this.status.lastError = e.message; log.warn(`STT 실패 (${u.id}, ${attempts}회): ${e.message}`); diff --git a/app/src/main/main.ts b/app/src/main/main.ts index 247f34a..b1e1eb7 100644 --- a/app/src/main/main.ts +++ b/app/src/main/main.ts @@ -34,6 +34,8 @@ let settings: SettingsStore | null = null; let auth: AppAuth | null = null; let win: BrowserWindow | null = null; let tray: Tray | null = null; +/** 트레이 · 상태 갱신 타이머 (종료할 때 멈춘다) */ +const timers: NodeJS.Timeout[] = []; let quitting = false; const dataDir = resolveDataDir(opt('data-dir')); @@ -148,7 +150,8 @@ function closeSplash(): void { } function updateTray(): void { - if (!tray) return; + // Windows 로그오프 등으로 Electron 이 트레이를 먼저 없앤 경우에도 오류 창이 뜨지 않게 + if (!tray || tray.isDestroyed()) return; if (!core) { tray.setImage(trayImage('off')); tray.setToolTip(`겜녹이\n${auth?.state.status === 'not_registered' ? '등록되지 않은 아이디' : '로그인이 필요합니다'}`); @@ -168,7 +171,7 @@ function updateTray(): void { } function buildTrayMenu(): void { - if (!tray) return; + if (!tray || tray.isDestroyed()) return; if (!core) { tray.setContextMenu( Menu.buildFromTemplate([ @@ -203,7 +206,11 @@ async function quit(): Promise { } catch (e) { log.error('종료 중 오류', e); } - tray?.destroy(); + // 트레이 갱신 타이머가 app.exit 사이에 한 번 더 돌면 'Tray is destroyed' 오류 창이 뜬다 + for (const t of timers) clearInterval(t); + const tr = tray; + tray = null; + tr?.destroy(); app.exit(0); } @@ -383,18 +390,18 @@ app.whenReady().then(async () => { // 시작할 때 확인 → 10분마다 다시 확인 (등록 해제되면 바로 잠김). 서버에 못 붙어 막힌 상태면 1분마다 다시 시도 let lastCheck = Date.now(); const first = a.check(); - setInterval(() => { + timers.push(setInterval(() => { const st = a.state.status; if ((st === 'ok' && Date.now() - lastCheck > 10 * 60000) || (st === 'error' && Date.now() - lastCheck > 60000) || (st === 'ok' && a.state.offline && Date.now() - lastCheck > 60000)) { lastCheck = Date.now(); void a.check().then(syncNames); } - }, 15000); - setInterval(() => { + }, 15000)); + timers.push(setInterval(() => { updateTray(); buildTrayMenu(); pushState(); - }, 1500); + }, 1500)); splashText('로그인 확인 중…'); await first; if (a.ok) splashText('녹음 · 음성 인식 엔진 시작 중…'); diff --git a/app/test/stt-shutdown.test.ts b/app/test/stt-shutdown.test.ts new file mode 100644 index 0000000..8404eeb --- /dev/null +++ b/app/test/stt-shutdown.test.ts @@ -0,0 +1,54 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { EventEmitter } from 'node:events'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { Store } from '../src/core/db/db.ts'; +import { Timeline } from '../src/core/timeline.ts'; +import { Bus } from '../src/core/util/events.ts'; +import { SttPipeline } from '../src/core/stt/pipeline.ts'; +import { WorkerError } from '../src/core/stt/worker.ts'; + +test('앱 종료: 변환 중이던 작업이 워커 종료로 실패해도 닫힌 DB 를 건드리지 않고, 남은 작업을 꺼내지 않는다', async () => { + const dir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-stop-')); + const store = new Store(path.join(dir, 'db.sqlite')); + store.upsertUser({ id: 'u1', name: '하늘', avatar: null }); + for (const id of ['s1', 's2']) { + const f = path.join(dir, `${id}.ogg`); + fs.writeFileSync(f, Buffer.concat([Buffer.from('OggS'), Buffer.alloc(50)])); + store.insertUtterance({ id, session_id: null, user_id: 'u1', started_at: Date.now(), ended_at: Date.now() + 1000, audio_path: f, match_id: null }); + } + const transcribing: string[] = []; + let fail: ((e: Error) => void) | null = null; + const worker: any = Object.assign(new EventEmitter(), { + ready: true, + request: async (op: string, req: any) => { + if (op === 'models') return { models: [{ name: 'm', installed: true }] }; + if (op !== 'transcribe') return {}; + transcribing.push(path.basename(req.path, '.ogg')); + return new Promise((_, rej) => (fail = rej)); + }, + }); + const settings: any = () => ({ stt: { realtimeModel: 'm', preciseModel: 'm' }, audio: { targetLufs: -18, noiseSensitivity: 0.5, denoise: false } }); + const dict: any = { hotwords: () => '', corrections: () => [], candidates: () => [] }; + const stt = new SttPipeline(worker, store, dict, new Timeline(store), new Bus(), settings); + await stt.load('realtime', 'm'); + const done: string[] = []; + stt.onDone = (id) => { + done.push(id); + store.getUtterance(id); // 실제 앱의 녹음 파일 이름 맞추기도 DB 를 읽는다 + }; + stt.enqueue('s1', 'realtime'); + stt.enqueue('s2', 'realtime'); + assert.deepEqual(transcribing, ['s1']); + + // core.stop 순서: stt.stop → 워커 종료 → DB 닫기 → (워커 프로세스 종료 이벤트로) 변환 요청 실패 + stt.stop(); + store.close(); + fail!(new WorkerError('worker_exit', '워커 종료')); + await new Promise((r) => setTimeout(r, 50)); + assert.deepEqual(done, [], '종료 중에는 변환 뒤 처리를 하지 않는다'); + assert.deepEqual(transcribing, ['s1']); + assert.equal(stt.status.queue, 1, '종료 중에는 다음 작업을 꺼내지 않는다 (다음 시작 때 다시 처리)'); +});