@@ -36,6 +36,10 @@ export function wrap_as_cancelable<T>(inner: Promise<T>): ControllablePromise<T>
3636}
3737
3838export function mono_wasm_cancel_promise ( task_holder_gc_handle : GCHandle ) : void {
39+ if ( ! loaderHelpers . is_runtime_running ( ) ) {
40+ mono_log_debug ( "This promise can't be canceled, mono runtime already exited." ) ;
41+ return ;
42+ }
3943 const holder = _lookup_js_owned_object ( task_holder_gc_handle ) as PromiseHolder ;
4044 mono_assert ( ! ! holder , ( ) => `Expected Promise for GCHandle ${ task_holder_gc_handle } ` ) ;
4145 holder . cancel ( ) ;
@@ -75,6 +79,11 @@ export class PromiseHolder extends ManagedObject {
7579
7680 resolve ( data : any ) {
7781 mono_assert ( ! this . isResolved , "resolve could be called only once" ) ;
82+ mono_assert ( ! this . isDisposed , "resolve is already disposed." ) ;
83+ if ( ! loaderHelpers . is_runtime_running ( ) ) {
84+ mono_log_debug ( "This promise resolution can't be propagated to managed code, mono runtime already exited." ) ;
85+ return ;
86+ }
7887 if ( WasmEnableThreads && ! this . setIsResolving ( ) ) {
7988 // we know that cancelation is in flight
8089 // because we need to keep the GCHandle alive until until the cancelation arrives
@@ -89,11 +98,16 @@ export class PromiseHolder extends ManagedObject {
8998 return ;
9099 }
91100 this . isResolved = true ;
92- this . complete_task ( data , null ) ;
101+ this . complete_task_wrapper ( data , null ) ;
93102 }
94103
95104 reject ( reason : any ) {
96105 mono_assert ( ! this . isResolved , "reject could be called only once" ) ;
106+ mono_assert ( ! this . isDisposed , "resolve is already disposed." ) ;
107+ if ( ! loaderHelpers . is_runtime_running ( ) ) {
108+ mono_log_debug ( "This promise rejection can't be propagated to managed code, mono runtime already exited." ) ;
109+ return ;
110+ }
97111 const isCancelation = reason && reason [ promise_holder_symbol ] === this ;
98112 if ( WasmEnableThreads && ! isCancelation && ! this . setIsResolving ( ) ) {
99113 // we know that cancelation is in flight
@@ -109,21 +123,22 @@ export class PromiseHolder extends ManagedObject {
109123 return ;
110124 }
111125 this . isResolved = true ;
112- this . complete_task ( null , reason ) ;
126+ this . complete_task_wrapper ( null , reason ) ;
113127 }
114128
115129 cancel ( ) {
116130 mono_assert ( ! this . isResolved , "cancel could be called only once" ) ;
131+ mono_assert ( ! this . isDisposed , "resolve is already disposed." ) ;
117132
118133 if ( this . isPostponed ) {
119134 // there was racing resolve/reject which was postponed, to retain valid GCHandle
120135 // in this case we just finish the original resolve/reject
121136 // and we need to use the postponed data/reason
122137 this . isResolved = true ;
123138 if ( this . reason !== undefined ) {
124- this . complete_task ( null , this . reason ) ;
139+ this . complete_task_wrapper ( null , this . reason ) ;
125140 } else {
126- this . complete_task ( this . data , null ) ;
141+ this . complete_task_wrapper ( this . data , null ) ;
127142 }
128143 } else {
129144 // there is no racing resolve/reject, we can reject/cancel the promise
@@ -138,11 +153,7 @@ export class PromiseHolder extends ManagedObject {
138153 }
139154
140155 // we can do this just once, because it will be dispose the GCHandle
141- complete_task ( data : any , reason : any ) {
142- if ( ! loaderHelpers . is_runtime_running ( ) ) {
143- mono_log_debug ( "This promise can't be propagated to managed code, mono runtime already exited." ) ;
144- return ;
145- }
156+ complete_task_wrapper ( data : any , reason : any ) {
146157 try {
147158 mono_assert ( ! this . isPosted , "Promise is already posted to managed." ) ;
148159 this . isPosted = true ;
0 commit comments