Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Wait for local app database closure before reopening

CloudTianTian193e31045b26
diff --git a/src/main/features/web_apps/runtime.ts b/src/main/features/web_apps/runtime.ts
index 7ee8dfbf..6cfb748a 100644
--- a/src/main/features/web_apps/runtime.ts
+++ b/src/main/features/web_apps/runtime.ts
@@ -173,16 +173,28 @@ export class WebAppRuntime {
private dataConnections=new Map<string,LocalAppData>();
private dataOwners=new Map<string,AppDataOwner>();
private dataOpening=new Map<string,Promise<LocalAppData>>();
- private dataClosures=new Set<Promise<void>>();
- async drainData(){await Promise.allSettled([...this.dataOpening.values()]);await Promise.allSettled([...this.dataClosures]);}
+ private dataClosures=new Map<string,Promise<void>>();
+ async drainData(){await Promise.allSettled([...this.dataOpening.values()]);await Promise.allSettled([...this.dataClosures.values()]);}
private dataFile(s:Instance){return this.preview ? path.join(this.preview.storageRoot,'data.sqlite') : path.join(webAppSandboxRoot(s.uid,s.bundle.key,'local'),'data.sqlite');}
private async dataCall(s:Instance,method:string,args:unknown,signal:AbortSignal){
if(heldAppData.has(JSON.stringify([s.uid,s.bundle.key])))return error('E_BUSY');
if(!s.manifest.data)return error('E_NOT_DECLARED');
- const file=this.dataFile(s);let connection=this.dataConnections.get(file);
+ const file=this.dataFile(s);
+ let connection=this.dataConnections.get(file);
if(this.dataOwners.get(file)?.closed){this.releaseData(file);connection=undefined;}
// A worker stopped by a call timeout or crash never recovers; release it so this call reopens.
if(connection?.dead){createLogger('web-app-data').warn('data worker stopped, reopening');this.releaseData(file);connection=undefined;}
+ // Both a user close and a stopped worker release ownership asynchronously.
+ // A new session must wait for that same file's final checkpoint first.
+ if(!connection){
+ const closing=this.dataClosures.get(file);
+ if(closing){
+ await closing;
+ if(signal.aborted||s.controller.signal.aborted||s.uid!==this.host.activeUser())return error('E_CLOSED');
+ if(heldAppData.has(JSON.stringify([s.uid,s.bundle.key])))return error('E_BUSY');
+ connection=this.dataConnections.get(file);
+ }
+ }
if(!connection){
if(!this.dataOpening.has(file)&&this.dataConnections.size+this.dataOpening.size>=4)return error('E_BUSY');
const boundary=this.preview?.storageRoot ?? userLocalRoot(s.uid);
@@ -205,7 +217,7 @@ export class WebAppRuntime {
private releaseData(file:string){
const connection=this.dataConnections.get(file);this.dataConnections.delete(file);
const owner=this.dataOwners.get(file);this.dataOwners.delete(file);
- if(connection){const closing=(owner?owner.close():connection.close()).catch(e=>{createLogger('web-app-data').warn('data checkpoint unavailable',{error_code:(e as any).code||'E_DATA'});}).finally(()=>this.dataClosures.delete(closing));this.dataClosures.add(closing);}
+ if(connection){const closing=(owner?owner.close():connection.close()).catch(e=>{createLogger('web-app-data').warn('data checkpoint unavailable',{error_code:(e as any).code||'E_DATA'});}).finally(()=>{if(this.dataClosures.get(file)===closing)this.dataClosures.delete(file);});this.dataClosures.set(file,closing);}
}
private instances = new Map<string, Instance>();
diff --git a/test/main/features/web_apps.test.ts b/test/main/features/web_apps.test.ts
index 1ee2f8d2..9883618b 100644
--- a/test/main/features/web_apps.test.ts
+++ b/test/main/features/web_apps.test.ts
@@ -35,6 +35,64 @@ afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});
describe('Web app capability journeys', () => {
+ it('waits for the same local database to close while keeping other apps usable', async () => {
+ const { LocalAppData } = await import('../../../src/main/features/web_apps/data');
+ const definition = { version: 1, tables: [{ id: 'notes', name: 'notes', fields: [{ id: 'body', name: 'body', type: 'string' }], access: { read: ['body'], insert: ['body'], count: true } }] };
+ const dataBundle = (name: string) => ({ ...bundle(name, ['data']), manifest: { sdkVersion: 1, capabilities: ['data'], data: definition } });
+ const b = dataBundle('closing-local-data');
+ const first = runtime.open(uid, 1, b);
+ await call(first.token, 'data.insert', { table: 'notes', values: { body: 'KEEP' } });
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const original = LocalAppData.prototype.close;
+ const close = vi.spyOn(LocalAppData.prototype, 'close').mockImplementationOnce(async function () {
+ await gate; await original.call(this);
+ });
+ try {
+ runtime.closeOwner(1);
+ const second = runtime.open(uid, 2, b);
+ let settled = false;
+ const reopened = runtime.call(uid, 2, second.token, 'reopened-query', 'data.query', { table: 'notes' })
+ .finally(() => { settled = true; });
+ const other = runtime.open(uid, 1, dataBundle('independent-local-data'));
+ expect(await call(other.token, 'data.insert', { table: 'notes', values: { body: 'OTHER' } })).toBeDefined();
+ expect(settled).toBe(false);
+ release();
+ expect(await reopened).toEqual({ records: [{ body: 'KEEP' }] });
+ await runtime.call(uid, 2, second.token, 'reopened-insert', 'data.insert', { table: 'notes', values: { body: 'NEW' } });
+ runtime.closeOwner(2); await runtime.drainData();
+ const third = runtime.open(uid, 2, b);
+ const result: any = await runtime.call(uid, 2, third.token, 'final-read', 'data.query', { table: 'notes' });
+ expect(result.records.map((row: any) => row.body).sort()).toEqual(['KEEP', 'NEW']);
+ } finally { release(); close.mockRestore(); }
+ expect(host.generate).not.toHaveBeenCalled(); expect(host.tools).not.toHaveBeenCalled();
+ });
+
+ it.each(['cancel', 'request-cancel', 'account-switch'] as const)('does not reopen private data when a waiting app loses access through %s', async action => {
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const dataCall = vi.fn(async () => ({ records: [] }));
+ const opening = vi.fn(async () => ({ call: dataCall } as any));
+ const create = vi.fn(() => ({ open: opening, call: dataCall, close: () => gate }));
+ host.dataOwner = create;
+ const b = { ...bundle('cancel-reopen', ['data']), manifest: { sdkVersion: 1, capabilities: ['data'], data: { version: 1, tables: [{ id: 'notes', name: 'notes', fields: [{ id: 'body', name: 'body', type: 'string' }], access: { read: ['body'] } }] } } };
+ const first = runtime.open(uid, 1, b);
+ await call(first.token, 'data.query', { table: 'notes' });
+ runtime.closeOwner(1);
+ const second = runtime.open(uid, 2, b);
+ const pending = runtime.call(uid, 2, second.token, 'waiting-read', 'data.query', { table: 'notes' })
+ .then(value => ({ value }), error => ({ error }));
+ await new Promise<void>(resolve => setImmediate(resolve));
+ if (action === 'cancel') runtime.closeOwner(2);
+ else if (action === 'request-cancel') runtime.cancel(uid, 2, second.token, 'waiting-read');
+ else uid = 'account-b';
+ release();
+ expect(await pending).toMatchObject({ error: { code: action === 'account-switch' ? 'E_CLOSED' : 'E_CANCELLED' } });
+ expect(create).toHaveBeenCalledOnce();
+ expect(dataCall).toHaveBeenCalledOnce();
+ });
+
+
it('keeps another application instance usable when the first requester closes during shared SQLite startup', async () => {
let enter!: () => void, release!: () => void;
const entered = new Promise<void>(resolve => { enter = resolve; });