diff --git a/src/bun.js/bindings/webcore/MessagePortChannelRegistry.cpp b/src/bun.js/bindings/webcore/MessagePortChannelRegistry.cpp index 427f3f3389..123c4425c7 100644 --- a/src/bun.js/bindings/webcore/MessagePortChannelRegistry.cpp +++ b/src/bun.js/bindings/webcore/MessagePortChannelRegistry.cpp @@ -47,6 +47,12 @@ MessagePortChannelRegistry::~MessagePortChannelRegistry() ASSERT(m_openChannels.isEmpty()); } +RefPtr MessagePortChannelRegistry::channelForPort(const MessagePortIdentifier& port) +{ + Locker locker { m_openChannelsLock }; + return m_openChannels.get(port); +} + void MessagePortChannelRegistry::didCreateMessagePortChannel(const MessagePortIdentifier& port1, const MessagePortIdentifier& port2) { // LOG(MessagePorts, "Registry: Creating MessagePortChannel %p linking %s and %s", this, port1.logString().utf8().data(), port2.logString().utf8().data()); @@ -86,11 +92,7 @@ void MessagePortChannelRegistry::didEntangleLocalToRemote(const MessagePortIdent // ASSERT(isMainThread()); // The channel might be gone if the remote side was closed. - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(local); - } + auto channel = channelForPort(local); if (!channel) return; @@ -104,12 +106,7 @@ void MessagePortChannelRegistry::didDisentangleMessagePort(const MessagePortIden // ASSERT(isMainThread()); // The channel might be gone if the remote side was closed. - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(port); - } - if (channel) + if (auto channel = channelForPort(port)) channel->disentanglePort(port); } @@ -119,11 +116,7 @@ void MessagePortChannelRegistry::didCloseMessagePort(const MessagePortIdentifier // LOG(MessagePorts, "Registry: MessagePort %s closed in registry", port.logString().utf8().data()); - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(port); - } + auto channel = channelForPort(port); if (!channel) return; @@ -145,11 +138,7 @@ bool MessagePortChannelRegistry::didPostMessageToRemote(MessageWithMessagePorts& // LOG(MessagePorts, "Registry: Posting message to MessagePort %s in registry", remoteTarget.logString().utf8().data()); // The channel might be gone if the remote side was closed. - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(remoteTarget); - } + auto channel = channelForPort(remoteTarget); if (!channel) { // LOG(MessagePorts, "Registry: Could not find MessagePortChannel for port %s; It was probably closed. Message will be dropped.", remoteTarget.logString().utf8().data()); return false; @@ -163,11 +152,7 @@ void MessagePortChannelRegistry::takeAllMessagesForPort(const MessagePortIdentif // ASSERT(isMainThread()); // The channel might be gone if the remote side was closed. - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(port); - } + auto channel = channelForPort(port); if (!channel) { callback({}, [] {}); return; @@ -183,11 +168,7 @@ std::optional MessagePortChannelRegistry::tryTakeMessag // LOG(MessagePorts, "Registry: Trying to take a message for MessagePort %s", port.logString().utf8().data()); // The channel might be gone if the remote side was closed. - RefPtr channel; - { - Locker locker { m_openChannelsLock }; - channel = m_openChannels.get(port); - } + auto channel = channelForPort(port); if (!channel) return std::nullopt; diff --git a/src/bun.js/bindings/webcore/MessagePortChannelRegistry.h b/src/bun.js/bindings/webcore/MessagePortChannelRegistry.h index a6e167933b..fd1321e162 100644 --- a/src/bun.js/bindings/webcore/MessagePortChannelRegistry.h +++ b/src/bun.js/bindings/webcore/MessagePortChannelRegistry.h @@ -58,6 +58,8 @@ public: WEBCORE_EXPORT void messagePortChannelDestroyed(MessagePortChannel&); private: + RefPtr channelForPort(const MessagePortIdentifier&); + Lock m_openChannelsLock; UncheckedKeyHashMap> m_openChannels WTF_GUARDED_BY_LOCK(m_openChannelsLock); };