@@ -22,6 +22,8 @@ import { assert, StringToUint8Array, Uint8ArrayToString } from "./support";
2222import { detectGPUDevice } from "./webgpu" ;
2323import * as compact from "./compact" ;
2424import * as runtime from "./runtime" ;
25+ import { timeStamp } from "console" ;
26+ import { Disposable } from "./types" ;
2527
2628enum RPCServerState {
2729 InitHeader ,
@@ -83,6 +85,7 @@ export class RPCServer {
8385 private pendingSend : Promise < void > = Promise . resolve ( ) ;
8486 private name : string ;
8587 private inst ?: runtime . Instance = undefined ;
88+ private globalObjects : Array < Disposable > = [ ] ;
8689 private serverRecvData ?: ( header : Uint8Array , body : Uint8Array ) => void ;
8790 private currPacketHeader ?: Uint8Array ;
8891 private currPacketLength = 0 ;
@@ -121,6 +124,9 @@ export class RPCServer {
121124 // eslint-disable-next-line @typescript-eslint/no-unused-vars
122125 private onClose ( _event : CloseEvent ) : void {
123126 if ( this . inst !== undefined ) {
127+ this . globalObjects . forEach ( obj => {
128+ obj . dispose ( ) ;
129+ } ) ;
124130 this . inst . dispose ( ) ;
125131 }
126132 if ( this . state == RPCServerState . ReceivePacketHeader ) {
@@ -263,6 +269,9 @@ export class RPCServer {
263269 }
264270
265271 this . inst = inst ;
272+ // begin scope to allow handling of objects
273+ // the object should stay alive during all sessions.
274+ this . inst . beginScope ( ) ;
266275 const fcreate = this . inst . getGlobalFunc ( "rpc.CreateEventDrivenServer" ) ;
267276
268277 const messageHandler = fcreate (
@@ -301,8 +310,10 @@ export class RPCServer {
301310 this . name ,
302311 this . key
303312 ) ;
304-
305- fcreate . dispose ( ) ;
313+ // message handler should persist across RPC runs
314+ this . globalObjects . push (
315+ this . inst . detachFromCurrentScope ( messageHandler )
316+ ) ;
306317 const writeFlag = this . inst . scalar ( 3 , "int32" ) ;
307318
308319 this . serverRecvData = ( header : Uint8Array , body : Uint8Array ) : void => {
@@ -320,7 +331,6 @@ export class RPCServer {
320331 // register the callback to redirect the session to local.
321332 const flocal = this . inst . getGlobalFunc ( "wasm.LocalSession" ) ;
322333 const localSession = flocal ( ) ;
323- flocal . dispose ( ) ;
324334 assert ( localSession instanceof runtime . Module ) ;
325335
326336 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -333,13 +343,14 @@ export class RPCServer {
333343 ) ;
334344 messageHandler ( header , writeFlag ) ;
335345 messageHandler ( body , writeFlag ) ;
336- localSession . dispose ( ) ;
337346
338347 this . log ( "Finish initializing the Wasm Server.." ) ;
339348 this . requestBytes ( SizeOf . I64 ) ;
340349 this . state = RPCServerState . ReceivePacketHeader ;
341350 // call process events in case there are bufferred data.
342351 this . processEvents ( ) ;
352+ // recycle all values.
353+ this . inst . endScope ( ) ;
343354 } ;
344355
345356 this . state = RPCServerState . WaitForCallback ;
0 commit comments