@@ -219,20 +219,31 @@ export class WorkflowHandle extends RpcTarget implements WorkflowInstance {
219219 public async status ( ) : Promise <
220220 InstanceStatus & { __LOCAL_DEV_STEP_OUTPUTS : unknown [ ] }
221221 > {
222- // If the stub is stale (e.g. after pause/restart/terminate aborted
223- // the DO), refresh it transparently so callers never see the error.
224- let status : EngineInstanceStatus ;
222+ // Both getStatus() and readLogs() must use the same fresh stub.
223+ // After pause/restart/terminate aborts the DO, the stub goes stale
224+ const fetchStatusAndLogs = async ( ) => {
225+ const status = await this . stub . getStatus ( ) ;
226+
227+ // NOTE(lduarte): for some reason, sync functions over RPC are typed as never instead of Promise<EngineLogs>
228+ const logs = await ( this . stub . readLogs ( ) as unknown as Promise <
229+ EngineLogs & Disposable
230+ > ) ;
231+
232+ return { status, logs } ;
233+ } ;
234+
235+ let result : {
236+ status : EngineInstanceStatus ;
237+ logs : EngineLogs & Disposable ;
238+ } ;
225239 try {
226- status = await this . stub . getStatus ( ) ;
240+ result = await fetchStatusAndLogs ( ) ;
227241 } catch {
228242 this . stub = this . getStub ( ) ;
229- status = await this . stub . getStatus ( ) ;
243+ result = await fetchStatusAndLogs ( ) ;
230244 }
231-
232- // NOTE(lduarte): for some reason, sync functions over RPC are typed as never instead of Promise<EngineLogs>
233- using logs = await ( this . stub . readLogs ( ) as unknown as Promise <
234- EngineLogs & Disposable
235- > ) ;
245+ // Dispose the RPC handle when the method scope exits
246+ using logs = result . logs ;
236247
237248 const filteredLogs = logs . logs . filter (
238249 ( log ) =>
@@ -255,7 +266,7 @@ export class WorkflowHandle extends RpcTarget implements WorkflowInstance {
255266 ) ?. metadata . error ;
256267
257268 return {
258- status : instanceStatusName ( status ) ,
269+ status : instanceStatusName ( result . status ) ,
259270 __LOCAL_DEV_STEP_OUTPUTS : stepOutputs ,
260271 output : workflowOutput ,
261272 error : workflowError ,
0 commit comments