Skip to content

Commit

Permalink
Revert "Try to take lock when delivering websocket events"
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea committed Jan 30, 2024
1 parent fa6bac1 commit fe67bd8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
16 changes: 4 additions & 12 deletions src/workerd/api/web-socket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ WebSocket::WebSocket(jsg::Lock& js,
kj::mv(KJ_REQUIRE_NONNULL(package.maybeTags)),
package.closedOutgoingConnection)),
outgoingMessages(IoContext::current().addObject(kj::heap<OutgoingMessagesMap>())),
locality(LOCAL),
maybeCriticalSection(IoContext::current().getCriticalSection()) {}
locality(LOCAL) {}
// This constructor is used when reinstantiating a websocket that had been hibernating, which is
// why we can go straight to the Accepted state. However, note that we are actually in the
// `Hibernatable` "sub-state"!
Expand All @@ -74,8 +73,7 @@ WebSocket::WebSocket(kj::Own<kj::WebSocket> native, Locality locality)
: url(kj::none),
farNative(nullptr),
outgoingMessages(IoContext::current().addObject(kj::heap<OutgoingMessagesMap>())),
locality(locality),
maybeCriticalSection(IoContext::current().getCriticalSection()) {
locality(locality) {
auto nativeObj = kj::heap<Native>();
nativeObj->state.init<AwaitingAcceptanceOrCoupling>(kj::mv(native));
farNative = IoContext::current().addObject(kj::mv(nativeObj));
Expand All @@ -85,8 +83,7 @@ WebSocket::WebSocket(kj::String url, Locality locality)
: url(kj::mv(url)),
farNative(nullptr),
outgoingMessages(IoContext::current().addObject(kj::heap<OutgoingMessagesMap>())),
locality(locality),
maybeCriticalSection(IoContext::current().getCriticalSection()) {
locality(locality) {
auto nativeObj = kj::heap<Native>();
nativeObj->state.init<AwaitingConnection>();
farNative = IoContext::current().addObject(kj::mv(nativeObj));
Expand Down Expand Up @@ -919,11 +916,6 @@ kj::Promise<kj::Maybe<kj::Exception>> WebSocket::readLoop() {
a.getMetrics().receivedWebSocketMessage(size);
}

// If we called from a blockConcurrencyWhile callback, we need to get the critical section
// lock so this event counts as part of the callback. If we run it as a separate event,
// it will need to wait for the blockConcurrencyWhile to finish, but that event will be
// waiting on this new event resulting in a deadlock.
//
// Re-enter the context with context.run(). This is arguably a bit unusual compared to other
// I/O which is delivered by return from context.awaitIo(), but the difference here is that we
// have a long stream of events over time. It makes sense to use context.run() each time a new
Expand Down Expand Up @@ -955,7 +947,7 @@ kj::Promise<kj::Maybe<kj::Exception>> WebSocket::readLoop() {
}

return true;
}, mapAddRef(maybeCriticalSection));
});

if (!result) co_return kj::none;
}
Expand Down
4 changes: 0 additions & 4 deletions src/workerd/api/web-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,6 @@ class WebSocket: public EventTarget {
kj::Maybe<kj::String> extensions;
};

// If we created this WebSocket inside a critical section (ex. a blockConcurrencyWhile callback)
// then we need to get the InputGate::Lock and pass it to context.run() when delivering events.
kj::Maybe<InputGate::CriticalSection&> maybeCriticalSection;

// So that each end of a WebSocketPair can keep track of its pair.
kj::Maybe<jsg::Ref<WebSocket>> maybePair;

Expand Down

0 comments on commit fe67bd8

Please sign in to comment.