Skip to content

Commit b6c23e0

Browse files
committed
fix yet another bug
1 parent 3902f8c commit b6c23e0

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

packages/ocap-kernel/src/remotes/network.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -590,24 +590,32 @@ export async function initNetwork(
590590
return;
591591
}
592592

593-
// Re-check connection limit after dial completes to prevent race conditions
594-
// Multiple concurrent dials could all pass the initial check, then all add channels
595-
try {
596-
checkConnectionLimit();
597-
} catch {
598-
// Connection limit reached - close the dialed channel and queue the message
599-
logger.log(
600-
`${targetPeerId}:: connection limit reached after dial, queueing message`,
601-
);
602-
// Explicitly close the channel to release network resources
603-
await connectionFactory.closeChannel(channel, targetPeerId);
604-
queue.enqueue(message);
605-
// Start reconnection to retry later when limit might free up
606-
handleConnectionLoss(targetPeerId);
607-
return;
608-
}
593+
// Check if a concurrent call already registered a channel for this peer
594+
// (dialIdempotent may return the same channel due to deduplication)
595+
const existingChannel = channels.get(targetPeerId);
596+
if (existingChannel) {
597+
// Another concurrent call already registered the channel, use it
598+
channel = existingChannel;
599+
} else {
600+
// Re-check connection limit after dial completes to prevent race conditions
601+
// Multiple concurrent dials could all pass the initial check, then all add channels
602+
try {
603+
checkConnectionLimit();
604+
} catch {
605+
// Connection limit reached - close the dialed channel and queue the message
606+
logger.log(
607+
`${targetPeerId}:: connection limit reached after dial, queueing message`,
608+
);
609+
// Explicitly close the channel to release network resources
610+
await connectionFactory.closeChannel(channel, targetPeerId);
611+
queue.enqueue(message);
612+
// Start reconnection to retry later when limit might free up
613+
handleConnectionLoss(targetPeerId);
614+
return;
615+
}
609616

610-
registerChannel(targetPeerId, channel);
617+
registerChannel(targetPeerId, channel);
618+
}
611619
} catch (problem) {
612620
outputError(targetPeerId, `opening connection`, problem);
613621
handleConnectionLoss(targetPeerId);

vitest.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default defineConfig({
130130
'packages/kernel-ui/**': {
131131
statements: 97.57,
132132
functions: 97.29,
133-
branches: 93.25,
133+
branches: 93.26,
134134
lines: 97.57,
135135
},
136136
'packages/kernel-utils/**': {
@@ -158,10 +158,10 @@ export default defineConfig({
158158
lines: 22.22,
159159
},
160160
'packages/ocap-kernel/**': {
161-
statements: 96.41,
161+
statements: 96.42,
162162
functions: 98.56,
163163
branches: 97.27,
164-
lines: 96.41,
164+
lines: 96.42,
165165
},
166166
'packages/omnium-gatherum/**': {
167167
statements: 5.67,

0 commit comments

Comments
 (0)