tests: fix TlsPreferTest flap on net481#1108
Conversation
The server task's ReadLineAsync races the client's failed TLS-handshake socket teardown: a FIN surfaces as null, an RST throws IOException. On net481/Windows the reset path is common. Detect TLS positively from the first byte (0x16 = Handshake record) instead of inferring from how the socket closed.
|
Claude finished @mtmk's task in 2m 15s —— View job PR Review
Approach is sound. Detecting TLS positively from the first byte ( One issue worth fixing:
Minor observation:
|
| int read; | ||
| try | ||
| { | ||
| read = await stream.ReadAsync(firstByte, 0, 1, cts.Token); |
There was a problem hiding this comment.
Only IOException is caught here, but stream.ReadAsync(…, cts.Token) can also throw OperationCanceledException when the 10-second CTS fires. If the test is slow (e.g. on a loaded CI agent) and the read hasn't completed before the timeout, the OCE propagates uncaught from the lambda, serverTask faults, and await serverTask on line 275 re-throws — producing a confusing test-failure message instead of a clean timeout.
Consider catching both, or catching the base type:
| read = await stream.ReadAsync(firstByte, 0, 1, cts.Token); | |
| try | |
| { | |
| read = await stream.ReadAsync(firstByte, 0, 1, cts.Token); | |
| } | |
| catch (Exception ex) when (ex is IOException or OperationCanceledException) | |
| { | |
| output.WriteLine($"[SERVER] RCV failed: {ex.Message}"); | |
| read = 0; | |
| } |
There was a problem hiding this comment.
this would still be confusing. not worth it.
Default ConnectTimeout is 2s, which is too tight for the fake-server handshake on slow CI runners (net481/Windows). Raise it to 10s to match the surrounding CancellationTokenSource.
The server task in
Prefer_mode_attempts_tls_upgrade_when_server_advertises_tls_availableraced the client's failed TLS-handshake socket teardown: a FIN surfaced asReadLineAsyncreturning null, while an RST threwIOException. On net481/Windows the reset path is common, producing the intermittent failure. Detect TLS positively from the first byte (0x16Handshake record) instead of inferring from how the socket closed.