Skip to content

Commit fa5feb0

Browse files
committed
fix issue with handling isolate cleanup
1 parent 340baad commit fa5feb0

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

dwds/lib/src/handlers/dev_handler.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ class DevHandler {
162162
);
163163
}
164164
}
165+
_logger.fine(
166+
'Sent request to $successfulSends clients out of ${_injectedConnections.length} total connections',
167+
);
165168
return successfulSends;
166169
}
167170

@@ -392,8 +395,12 @@ class DevHandler {
392395
_injectedConnections.remove(injectedConnection);
393396
final connection = appConnection;
394397
if (connection != null) {
395-
_appConnectionByAppId.remove(connection.request.appId);
396-
final services = _servicesByAppId[connection.request.appId];
398+
final appId = connection.request.appId;
399+
final services = _servicesByAppId[appId];
400+
// WebSocket mode doesn't need this because WebSocketProxyService handles connection tracking and cleanup
401+
if (!useWebSocketConnection) {
402+
_appConnectionByAppId.remove(appId);
403+
}
397404
if (services != null) {
398405
if (services.connectedInstanceId == null ||
399406
services.connectedInstanceId == connection.request.instanceId) {

dwds/lib/src/services/web_socket_proxy_service.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,31 @@ class WebSocketProxyService implements VmServiceInterface {
258258
_logger.fine(
259259
'Removed connection: $connectionId (remaining: $_activeConnectionCount)',
260260
);
261+
_logger.fine(
262+
'Current tracked connections: ${_appConnectionDoneSubscriptions.keys.toList()}',
263+
);
261264

262-
// Only destroy the isolate if there are no more active connections
265+
// Instead of destroying the isolate immediately, check if there are still
266+
// clients that can receive hot reload requests
263267
if (_activeConnectionCount <= 0) {
264-
_logger.fine('No more active connections, destroying isolate');
265-
destroyIsolate();
268+
// Double-check by asking the sendClientRequest callback how many clients are available
269+
final actualClientCount = sendClientRequest({'type': 'ping'});
270+
_logger.fine(
271+
'Actual client count from sendClientRequest: $actualClientCount',
272+
);
273+
274+
if (actualClientCount == 0) {
275+
_logger.fine(
276+
'No clients available for hot reload, destroying isolate',
277+
);
278+
destroyIsolate();
279+
} else {
280+
_logger.fine(
281+
'Still have $actualClientCount clients available, keeping isolate alive',
282+
);
283+
// Update our internal counter to match reality
284+
_activeConnectionCount = actualClientCount;
285+
}
266286
} else {
267287
_logger.fine(
268288
'Still have $_activeConnectionCount active connections, keeping isolate alive',

0 commit comments

Comments
 (0)