@@ -782,7 +782,14 @@ void WebSocket::serializeAttachment(jsg::Lock& js, jsg::JsValue attachment) {
782782void WebSocket::setAutoResponseStatus (
783783 kj::Maybe<kj::Date> time, kj::Promise<void > autoResponsePromise) {
784784 autoResponseTimestamp = time;
785- autoResponseStatus.ongoingAutoResponse = kj::mv (autoResponsePromise);
785+ KJ_IF_SOME (context, IoContext::tryCurrent ()) {
786+ autoResponseStatus.ongoingAutoResponse .emplace (
787+ context.addObject (kj::heap (kj::mv (autoResponsePromise))));
788+ } else {
789+ // Called outside an IoContext (e.g. from the hibernation manager's readLoop).
790+ // Use plain kj::Own; the caller manages the promise lifecycle.
791+ autoResponseStatus.ongoingAutoResponse .emplace (kj::heap (kj::mv (autoResponsePromise)));
792+ }
786793}
787794
788795kj::Maybe<kj::Date> WebSocket::getAutoResponseTimestamp () {
@@ -860,9 +867,14 @@ kj::Promise<void> WebSocket::sendAutoResponse(kj::String message, kj::WebSocket&
860867 autoResponseStatus.pendingAutoResponseDeque .push (kj::mv (message));
861868 } else if (!autoResponseStatus.isClosed ) {
862869 auto p = ws.send (message).fork ();
863- autoResponseStatus.ongoingAutoResponse = p.addBranch ();
870+ KJ_IF_SOME (context, IoContext::tryCurrent ()) {
871+ autoResponseStatus.ongoingAutoResponse .emplace (context.addObject (kj::heap (p.addBranch ())));
872+ } else {
873+ // Called outside an IoContext (e.g. from the hibernation manager's readLoop).
874+ autoResponseStatus.ongoingAutoResponse .emplace (kj::heap (p.addBranch ()));
875+ }
864876 co_await p;
865- autoResponseStatus.ongoingAutoResponse = kj::READY_NOW ;
877+ autoResponseStatus.ongoingAutoResponse = kj::none ;
866878 }
867879}
868880
@@ -928,8 +940,17 @@ kj::Promise<void> WebSocket::pump(IoContext& context,
928940
929941 // If we have a ongoingAutoResponse, we must co_await it here because there's a ws.send()
930942 // in progress. Otherwise there can occur ws.send() race problems.
931- co_await autoResponse.ongoingAutoResponse ;
932- autoResponse.ongoingAutoResponse = kj::READY_NOW ;
943+ KJ_IF_SOME (promiseHolder, autoResponse.ongoingAutoResponse ) {
944+ KJ_SWITCH_ONEOF (promiseHolder) {
945+ KJ_CASE_ONEOF (ioOwned, IoOwn<kj::Promise<void >>) {
946+ co_await *ioOwned;
947+ }
948+ KJ_CASE_ONEOF (owned, kj::Own<kj::Promise<void >>) {
949+ co_await *owned;
950+ }
951+ }
952+ autoResponse.ongoingAutoResponse = kj::none;
953+ }
933954
934955 do {
935956 while (outgoingMessages.size () > 0 ) {
0 commit comments