You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: packages/core/src/actor/instance.ts
+50-7Lines changed: 50 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -158,6 +158,7 @@ export class ActorInstance<
158
158
#vars?: V;
159
159
160
160
#backgroundPromises: Promise<void>[]=[];
161
+
#abortController =newAbortController();
161
162
#config: ActorConfig<S,CP,CS,V,I,AD,DB>;
162
163
#connectionDrivers!: ConnectionDriversMap;
163
164
#actorDriver!: ActorDriver;
@@ -1050,6 +1051,9 @@ export class ActorInstance<
1050
1051
1051
1052
#assertReady(){
1052
1053
if(!this.#ready)thrownewerrors.InternalError("Actor not ready");
1054
+
if(this.#sleepCalled)
1055
+
thrownewerrors.InternalError("Actor is going to sleep");
1056
+
if(this.#stopCalled)thrownewerrors.InternalError("Actor is stopping");
1053
1057
}
1054
1058
1055
1059
/**
@@ -1443,24 +1447,24 @@ export class ActorInstance<
1443
1447
}
1444
1448
1445
1449
/**
1446
-
* Runs a promise in the background.
1450
+
* Prevents the actor from sleeping until promise is complete.
1447
1451
*
1448
1452
* This allows the actor runtime to ensure that a promise completes while
1449
1453
* returning from an action request early.
1450
1454
*
1451
1455
* @param promise - The promise to run in the background.
1452
1456
*/
1453
-
_runInBackground(promise: Promise<void>){
1457
+
_waitUntil(promise: Promise<void>){
1454
1458
this.#assertReady();
1455
1459
1456
1460
// TODO: Should we force save the state?
1457
1461
// Add logging to promise and make it non-failable
1458
1462
constnonfailablePromise=promise
1459
1463
.then(()=>{
1460
-
logger().debug("background promise complete");
1464
+
logger().debug("wait until promise complete");
1461
1465
})
1462
1466
.catch((error)=>{
1463
-
logger().error("background promise failed",{
1467
+
logger().error("wait until promise failed",{
1464
1468
error: stringifyError(error),
1465
1469
});
1466
1470
});
@@ -1552,7 +1556,7 @@ export class ActorInstance<
1552
1556
returntrue;
1553
1557
}
1554
1558
1555
-
/** Puts an actor to sleep. */
1559
+
/** Puts an actor to sleep. This should just start the sleep sequence, most shutdown logic should be in _stop (which is called by the ActorDriver when sleeping). */
1556
1560
async_sleep(){
1557
1561
invariant(this.#sleepingSupported,"sleeping not supported");
1558
1562
invariant(this.#actorDriver.sleep,"no sleep on driver");
@@ -1581,6 +1585,11 @@ export class ActorInstance<
1581
1585
1582
1586
logger().info("actor stopping");
1583
1587
1588
+
// Abort any listeners waiting for shutdown
1589
+
try{
1590
+
this.#abortController.abort();
1591
+
}catch{}
1592
+
1584
1593
// Call onStop lifecycle hook if defined
1585
1594
if(this.#config.onStop){
1586
1595
try{
@@ -1601,6 +1610,9 @@ export class ActorInstance<
1601
1610
}
1602
1611
}
1603
1612
1613
+
// Wait for any background tasks to finish, with timeout
0 commit comments