|
| 1 | +// Copyright (c) 2024, the gRPC project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +import 'dart:async'; |
| 17 | + |
| 18 | +import 'package:grpc/grpc.dart'; |
| 19 | +import 'package:grpc/src/client/http2_connection.dart'; |
| 20 | +import 'package:grpc/src/server/handler.dart'; |
| 21 | +import 'package:grpc/src/shared/message.dart'; |
| 22 | +import 'package:http2/transport.dart'; |
| 23 | +import 'package:test/test.dart'; |
| 24 | + |
| 25 | +import 'src/server_utils.dart'; |
| 26 | +import 'src/utils.dart'; |
| 27 | + |
| 28 | +/// Service that triggers the race condition by having serialization errors |
| 29 | +class RaceConditionService extends Service { |
| 30 | + @override |
| 31 | + String get $name => 'RaceCondition'; |
| 32 | + |
| 33 | + bool shouldFailSerialization = false; |
| 34 | + |
| 35 | + RaceConditionService() { |
| 36 | + $addMethod(ServiceMethod<int, int>( |
| 37 | + 'StreamingMethod', |
| 38 | + streamingMethod, |
| 39 | + false, // not streaming request |
| 40 | + true, // streaming response |
| 41 | + mockDecode, |
| 42 | + (int value) { |
| 43 | + // Conditionally fail serialization to trigger the error path |
| 44 | + if (shouldFailSerialization && value > 2) { |
| 45 | + throw Exception('Simulated serialization error!'); |
| 46 | + } |
| 47 | + return mockEncode(value); |
| 48 | + }, |
| 49 | + )); |
| 50 | + } |
| 51 | + |
| 52 | + Stream<int> streamingMethod(ServiceCall call, Future<int> request) async* { |
| 53 | + // Send multiple responses |
| 54 | + for (int i = 0; i < 5; i++) { |
| 55 | + yield i; |
| 56 | + // Small delay to allow concurrent operations |
| 57 | + await Future.delayed(Duration(milliseconds: 1)); |
| 58 | + |
| 59 | + // After sending a few responses, trigger conditions that could close the stream |
| 60 | + if (i == 2) { |
| 61 | + shouldFailSerialization = true; |
| 62 | + // Also simulate a timeout or cancellation happening around the same time |
| 63 | + Timer(Duration(microseconds: 100), () { |
| 64 | + // This simulates what happens during a timeout/cancellation |
| 65 | + // The handler would normally close the requests stream |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Custom harness for testing the race condition |
| 73 | +class RaceConditionHarness { |
| 74 | + final toServer = StreamController<StreamMessage>(); |
| 75 | + final fromServer = StreamController<StreamMessage>(); |
| 76 | + final service = RaceConditionService(); |
| 77 | + late ConnectionServer server; |
| 78 | + |
| 79 | + // Track errors that occur in the handler |
| 80 | + final List<GrpcError> capturedErrors = []; |
| 81 | + StackTrace? capturedStackTrace; |
| 82 | + |
| 83 | + void setUp() { |
| 84 | + server = Server.create( |
| 85 | + services: [service], |
| 86 | + errorHandler: (error, stackTrace) { |
| 87 | + capturedErrors.add(error); |
| 88 | + capturedStackTrace = stackTrace; |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + final stream = TestServerStream(toServer.stream, fromServer.sink); |
| 93 | + server.serveStream_(stream: stream); |
| 94 | + } |
| 95 | + |
| 96 | + void tearDown() { |
| 97 | + fromServer.close(); |
| 98 | + toServer.close(); |
| 99 | + } |
| 100 | + |
| 101 | + void sendRequestHeader(String path) { |
| 102 | + final headers = Http2ClientConnection.createCallHeaders( |
| 103 | + true, |
| 104 | + 'test', |
| 105 | + path, |
| 106 | + Duration(seconds: 1), // Add a timeout |
| 107 | + null, |
| 108 | + null, |
| 109 | + userAgent: 'dart-grpc/test'); |
| 110 | + toServer.add(HeadersStreamMessage(headers)); |
| 111 | + } |
| 112 | + |
| 113 | + void sendData(int value) { |
| 114 | + toServer.add(DataStreamMessage(frame(mockEncode(value)))); |
| 115 | + } |
| 116 | + |
| 117 | + void closeClientStream() { |
| 118 | + toServer.add(HeadersStreamMessage([], endStream: true)); |
| 119 | + } |
| 120 | + |
| 121 | + void simulateClientDisconnect() { |
| 122 | + // Simulate abrupt client disconnect |
| 123 | + toServer.addError('Client disconnected'); |
| 124 | + toServer.close(); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void main() { |
| 129 | + group('Race Condition in ServerHandler', () { |
| 130 | + late RaceConditionHarness harness; |
| 131 | + |
| 132 | + setUp(() { |
| 133 | + harness = RaceConditionHarness(); |
| 134 | + harness.setUp(); |
| 135 | + }); |
| 136 | + |
| 137 | + tearDown(() { |
| 138 | + harness.tearDown(); |
| 139 | + }); |
| 140 | + |
| 141 | + test( |
| 142 | + 'Should handle serialization error without crashing when stream closes concurrently', |
| 143 | + () async { |
| 144 | + // Set up response listener |
| 145 | + final responseCompleter = Completer<void>(); |
| 146 | + int responseCount = 0; |
| 147 | + bool gotError = false; |
| 148 | + |
| 149 | + harness.fromServer.stream.listen( |
| 150 | + (message) { |
| 151 | + responseCount++; |
| 152 | + print( |
| 153 | + 'Received response message #$responseCount: ${message.runtimeType}'); |
| 154 | + }, |
| 155 | + onError: (error) { |
| 156 | + print('Response stream error: $error'); |
| 157 | + gotError = true; |
| 158 | + }, |
| 159 | + onDone: () { |
| 160 | + print('Response stream closed'); |
| 161 | + responseCompleter.complete(); |
| 162 | + }, |
| 163 | + ); |
| 164 | + |
| 165 | + // Send request |
| 166 | + harness.sendRequestHeader('/RaceCondition/StreamingMethod'); |
| 167 | + harness.sendData(1); |
| 168 | + |
| 169 | + // Wait for some responses to be processed |
| 170 | + await Future.delayed(Duration(milliseconds: 10)); |
| 171 | + |
| 172 | + // Now close the client stream while the server is still sending responses |
| 173 | + // This simulates a client disconnect/timeout happening during response serialization |
| 174 | + harness.closeClientStream(); |
| 175 | + |
| 176 | + // Wait for everything to complete |
| 177 | + await responseCompleter.future.timeout( |
| 178 | + Duration(seconds: 2), |
| 179 | + onTimeout: () { |
| 180 | + print('Test timed out waiting for response stream to close'); |
| 181 | + }, |
| 182 | + ); |
| 183 | + |
| 184 | + // Check if we captured any errors |
| 185 | + if (harness.capturedErrors.isNotEmpty) { |
| 186 | + print( |
| 187 | + 'Captured errors: ${harness.capturedErrors.map((e) => e.message)}'); |
| 188 | + |
| 189 | + // The important thing is that the server didn't crash with "Cannot add event after closing" |
| 190 | + // Check that we don't have the bad state error |
| 191 | + final hasBadStateError = harness.capturedErrors.any( |
| 192 | + (e) => e.message?.contains('Cannot add event after closing') ?? false); |
| 193 | + expect(hasBadStateError, isFalse, |
| 194 | + reason: 'Should not have "Cannot add event after closing" error'); |
| 195 | + } |
| 196 | + |
| 197 | + // The test passes if we reach here without an unhandled exception |
| 198 | + print('Test completed successfully without server crash'); |
| 199 | + }); |
| 200 | + |
| 201 | + test( |
| 202 | + 'Stress test - multiple concurrent disconnections during serialization errors', |
| 203 | + () async { |
| 204 | + // This test increases the likelihood of hitting the race condition |
| 205 | + final futures = <Future>[]; |
| 206 | + |
| 207 | + for (int i = 0; i < 10; i++) { |
| 208 | + futures.add(() async { |
| 209 | + final harness = RaceConditionHarness(); |
| 210 | + harness.setUp(); |
| 211 | + |
| 212 | + try { |
| 213 | + // Send request |
| 214 | + harness.sendRequestHeader('/RaceCondition/StreamingMethod'); |
| 215 | + harness.sendData(1); |
| 216 | + |
| 217 | + // Random delay before disconnect |
| 218 | + await Future.delayed(Duration(milliseconds: i % 5)); |
| 219 | + |
| 220 | + // Randomly choose how to disconnect |
| 221 | + if (i % 2 == 0) { |
| 222 | + harness.closeClientStream(); |
| 223 | + } else { |
| 224 | + harness.simulateClientDisconnect(); |
| 225 | + } |
| 226 | + |
| 227 | + // Wait a bit for any errors to manifest |
| 228 | + await Future.delayed(Duration(milliseconds: 10)); |
| 229 | + } finally { |
| 230 | + harness.tearDown(); |
| 231 | + } |
| 232 | + }()); |
| 233 | + } |
| 234 | + |
| 235 | + await Future.wait(futures); |
| 236 | + |
| 237 | + // The test passes if none of the iterations caused an unhandled exception |
| 238 | + print('Stress test completed without crashes'); |
| 239 | + }); |
| 240 | + |
| 241 | + test('Reproduce exact "Cannot add event after closing" scenario', () async { |
| 242 | + // This test specifically tries to reproduce the exact error message from production |
| 243 | + final errorCompleter = Completer<String>(); |
| 244 | + |
| 245 | + // Create a fresh harness for this test |
| 246 | + final testHarness = RaceConditionHarness(); |
| 247 | + |
| 248 | + // Override the error handler to capture the exact error |
| 249 | + final server = Server.create( |
| 250 | + services: [testHarness.service], |
| 251 | + errorHandler: (error, stackTrace) { |
| 252 | + print('Error handler called with: ${error.message}'); |
| 253 | + if (error.message?.contains('Cannot add event after closing') ?? |
| 254 | + false) { |
| 255 | + errorCompleter.complete('REPRODUCED: ${error.message}'); |
| 256 | + } |
| 257 | + }, |
| 258 | + ); |
| 259 | + |
| 260 | + final stream = |
| 261 | + TestServerStream(testHarness.toServer.stream, testHarness.fromServer.sink); |
| 262 | + server.serveStream_(stream: stream); |
| 263 | + |
| 264 | + // Send request that will trigger serialization errors |
| 265 | + harness.sendRequestHeader('/RaceCondition/StreamingMethod'); |
| 266 | + harness.sendData(1); |
| 267 | + |
| 268 | + // Wait for responses to start |
| 269 | + await Future.delayed(Duration(milliseconds: 5)); |
| 270 | + |
| 271 | + // Force close the stream while serialization error is happening |
| 272 | + harness.toServer.close(); |
| 273 | + |
| 274 | + // Check if we reproduced the error |
| 275 | + final result = await errorCompleter.future.timeout( |
| 276 | + Duration(milliseconds: 100), |
| 277 | + onTimeout: () => 'Did not reproduce the exact error', |
| 278 | + ); |
| 279 | + |
| 280 | + print('Result: $result'); |
| 281 | + |
| 282 | + // The test succeeds whether we reproduce it or not, |
| 283 | + // but we log if we successfully reproduced it |
| 284 | + if (result.startsWith('REPRODUCED')) { |
| 285 | + print('✓ Successfully reproduced the production error!'); |
| 286 | + print(' This confirms the race condition exists.'); |
| 287 | + } |
| 288 | + }); |
| 289 | + }); |
| 290 | +} |
0 commit comments