Skip to content

Commit 37d9508

Browse files
committed
fix(core): let evidence.query wait for slow queries in sandboxed blocks
1 parent 1e6a2da commit 37d9508

9 files changed

Lines changed: 62 additions & 16 deletions

File tree

‎cli/svelte.config.js‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/src/user-components/sandbox/request-response.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ describe('SandboxRpc requests (this end asks the peer)', () => {
8181
await assertion;
8282
});
8383

84+
it('waits past the default timeout for a reply when timeoutMs is null', async () => {
85+
vi.useFakeTimers();
86+
const { rpc } = makeRpc();
87+
const promise = rpc.request('slow', undefined, { timeoutMs: null });
88+
await vi.advanceTimersByTimeAsync(60 * 60 * 1000);
89+
90+
rpc.handleMessage({ type: 'rpc-response', requestId: 'id-1', ok: true, result: 'done' });
91+
await expect(promise).resolves.toBe('done');
92+
});
93+
94+
it('rejects a request with no timeout when the channel is disposed', async () => {
95+
const { rpc } = makeRpc();
96+
const promise = rpc.request('slow', undefined, { timeoutMs: null });
97+
98+
rpc.dispose();
99+
await expect(promise).rejects.toThrow('sandbox channel closed');
100+
});
101+
84102
it('ignores a late reply that arrives after the request timed out', async () => {
85103
vi.useFakeTimers();
86104
const { rpc } = makeRpc();

‎core/src/user-components/sandbox/request-response.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export interface SandboxRpcOptions {
5353
* passes plain objects through untouched.
5454
*/
5555
post: (message: Record<string, unknown>) => void;
56-
/** Default per-request timeout. 10s matches the old capture-png ceiling. */
56+
/**
57+
* Default per-request timeout. 10s matches the old capture-png ceiling.
58+
* Requests with no upper bound on their duration pass `timeoutMs: null`.
59+
*/
5760
defaultTimeoutMs?: number;
5861
/** Override id minting (tests inject a deterministic counter). */
5962
generateId?: () => string;
@@ -70,7 +73,8 @@ function defaultGenerateId(): string {
7073
interface Pending {
7174
resolve: (value: unknown) => void;
7275
reject: (error: Error) => void;
73-
timer: ReturnType<typeof setTimeout>;
76+
/** Unset for requests with `timeoutMs: null`. */
77+
timer?: ReturnType<typeof setTimeout>;
7478
}
7579

7680
export class SandboxRpc {
@@ -90,19 +94,27 @@ export class SandboxRpc {
9094
/**
9195
* Send a request and await the peer's reply. Rejects if the peer reports
9296
* an error, has no handler for `kind`, or doesn't reply within the timeout.
97+
* `timeoutMs: null` waits for the reply until the channel is disposed.
9398
*/
94-
request<T = unknown>(kind: string, payload?: unknown, opts?: { timeoutMs?: number }): Promise<T> {
99+
request<T = unknown>(
100+
kind: string,
101+
payload?: unknown,
102+
opts?: { timeoutMs?: number | null }
103+
): Promise<T> {
95104
if (this.#disposed) {
96105
return Promise.reject(new Error('sandbox channel closed'));
97106
}
98107
const requestId = this.#generateId();
99-
const timeoutMs = opts?.timeoutMs ?? this.#defaultTimeoutMs;
108+
const timeoutMs = opts?.timeoutMs === undefined ? this.#defaultTimeoutMs : opts.timeoutMs;
100109
return new Promise<T>((resolve, reject) => {
101-
const timer = setTimeout(() => {
102-
if (this.#pending.delete(requestId)) {
103-
reject(new Error(`sandbox request "${kind}" timed out after ${timeoutMs}ms`));
104-
}
105-
}, timeoutMs);
110+
const timer =
111+
timeoutMs === null
112+
? undefined
113+
: setTimeout(() => {
114+
if (this.#pending.delete(requestId)) {
115+
reject(new Error(`sandbox request "${kind}" timed out after ${timeoutMs}ms`));
116+
}
117+
}, timeoutMs);
106118
this.#pending.set(requestId, {
107119
resolve: resolve as (value: unknown) => void,
108120
reject,

‎core/src/user-components/sandbox/runtime-bootstrap.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface SandboxHost {
3434
request: <T = unknown>(
3535
kind: string,
3636
payload?: unknown,
37-
opts?: { timeoutMs?: number }
37+
opts?: { timeoutMs?: number | null }
3838
) => Promise<T>;
3939
}
4040

‎core/src/user-components/tags/custom_map/sandbox/map-evidence-sdk.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('createMapEvidenceSdk', () => {
4444
const { host, request } = makeHost();
4545
const sdk = createMapEvidenceSdk(host, INIT);
4646
const rows = await sdk.evidence.query('locations');
47-
expect(request).toHaveBeenCalledWith('query', { name: 'locations' });
47+
expect(request).toHaveBeenCalledWith('query', { name: 'locations' }, { timeoutMs: null });
4848
expect(rows).toEqual([{ a: 1 }]);
4949
});
5050

‎core/src/user-components/tags/custom_map/sandbox/map-evidence-sdk.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ export function createMapEvidenceSdk(host: SandboxHost, init: InitMessage): MapE
128128
'evidence.query(name): `name` must not be empty. Pass the query name, e.g. evidence.query("locations").'
129129
);
130130
}
131-
const response = await host.request<MapQueryResponse>(MAP_QUERY_REQUEST, { name });
131+
// No timeout: a slow warehouse query should wait, as it does in native
132+
// components. The channel still rejects this if the frame goes away.
133+
const response = await host.request<MapQueryResponse>(
134+
MAP_QUERY_REQUEST,
135+
{ name },
136+
{ timeoutMs: null }
137+
);
132138
return response?.rows ?? [];
133139
},
134140
get variables() {

‎core/src/user-components/tags/html/sandbox/evidence-sdk.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('createEvidenceSdk: query', () => {
3939

4040
const result = await evidence.query('orders');
4141

42-
expect(request).toHaveBeenCalledWith('query', { name: 'orders' });
42+
expect(request).toHaveBeenCalledWith('query', { name: 'orders' }, { timeoutMs: null });
4343
expect(result).toBe(rows);
4444
});
4545

‎core/src/user-components/tags/html/sandbox/evidence-sdk.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ export function createEvidenceSdk(host: SandboxHost, init: HtmlInitMessage): Evi
202202
'evidence.query(name): `name` must not be empty. Pass the query name, e.g. evidence.query("orders").'
203203
);
204204
}
205-
const response = await host.request<HtmlQueryResponse>(HTML_QUERY_REQUEST, { name });
205+
// No timeout: a slow warehouse query should wait, as it does in native
206+
// components. The channel still rejects this if the frame goes away.
207+
const response = await host.request<HtmlQueryResponse>(
208+
HTML_QUERY_REQUEST,
209+
{ name },
210+
{ timeoutMs: null }
211+
);
206212
return response?.rows ?? [];
207213
},
208214
get variables() {

‎core/svelte.config.js‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)