diff --git a/src/workerd/server/server.c++ b/src/workerd/server/server.c++ index 74b3b0b77977..54a376bd7a99 100644 --- a/src/workerd/server/server.c++ +++ b/src/workerd/server/server.c++ @@ -1378,8 +1378,8 @@ public: kj::defer([this, idCopy=kj::mv(idCopy)]() { // Once this WorkerInterface is dropped, we need to run some cleanup tasks. KJ_IF_SOME(entry, actors.find(idCopy)) { - entry->clientsConnected--; - if (entry->clientsConnected == 0) { + entry->clientDropped(); + if (!entry->hasClients()) { // No clients remain connected, if no new clients connect before the expiration // timeout, the actor will be removed by the cleanupLoop task. entry->updateAccessTime(); @@ -1478,9 +1478,12 @@ public: }); } + bool hasClients() { return clientsConnected != 0; } + void clientAdded() { ++clientsConnected; } + void clientDropped() { --clientsConnected; } + // The actor is constructed after the ActorContainer so it starts off empty. kj::Maybe> actor; - size_t clientsConnected = 0; private: kj::StringPtr key; kj::Own tracker; @@ -1489,6 +1492,7 @@ public: kj::TimePoint lastAccess; kj::Maybe> manager; bool hasHibernated = false; + size_t clientsConnected = 0; kj::Maybe> shutdownTask; }; @@ -1513,7 +1517,7 @@ public: // Need to start the cleanup loop. cleanupTask = cleanupLoop(); } - KJ_REQUIRE_NONNULL(actors.find(idCopy))->clientsConnected++; + KJ_REQUIRE_NONNULL(actors.find(idCopy))->clientAdded(); co_return service.startRequest(kj::mv(metadata), className, kj::mv(actor)); } @@ -1529,7 +1533,7 @@ public: while (true) { auto now = timer.now(); actors.eraseAll([&](auto&, kj::Own& entry) { - if (entry->clientsConnected != 0) { + if (entry->hasClients()) { // We are still using the actor so we cannot remove it! return false; }