Skip to content

Commit 8ecb87c

Browse files
authored
Merge pull request #1018 from Unity-Technologies/unity-master-fix-tls-hang-on-connection-close-during-readwrite
[tls] Fix read/write ignoring error code and requesting more read/write calls
2 parents e95a390 + acf07dc commit 8ecb87c

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

mcs/class/System/Mono.UnityTls/UnityTlsContext.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ public override void Flush ()
172172

173173
public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int count)
174174
{
175-
bool wantMore = false;
176175
int numBytesRead = 0;
177176

178177
lastException = null;
@@ -183,19 +182,27 @@ public override (int ret, bool wantMore) Read (byte[] buffer, int offset, int co
183182
if (lastException != null)
184183
throw lastException;
185184

186-
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK || numBytesRead < count) // In contrast to some other APIs (like Apple security) WOULD_BLOCK is not set if we did a partial read
187-
wantMore = true;
188-
else if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED)
189-
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
190-
else
191-
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to read data from TLS context");
185+
switch (errorState.code)
186+
{
187+
case UnityTls.unitytls_error_code.UNITYTLS_SUCCESS:
188+
// In contrast to some other APIs (like Apple security) WOULD_BLOCK is not set if we did a partial write.
189+
// The Mono Api however requires us to set the wantMore flag also if we didn't read all the data.
190+
return (numBytesRead, numBytesRead < count);
191+
192+
case UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK:
193+
return (numBytesRead, true);
194+
195+
case UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED:
196+
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
192197

193-
return (numBytesRead, wantMore);
198+
default:
199+
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to read data to TLS context");
200+
return (0, false);
201+
}
194202
}
195203

196204
public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int count)
197205
{
198-
bool wantMore = false;
199206
int numBytesWritten = 0;
200207

201208
lastException = null;
@@ -206,14 +213,23 @@ public override (int ret, bool wantMore) Write (byte[] buffer, int offset, int c
206213
if (lastException != null)
207214
throw lastException;
208215

209-
if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK || numBytesWritten < count) // In contrast to some other APIs (like Apple security) WOULD_BLOCK is not set if we did a partial write
210-
wantMore = true;
211-
else if (errorState.code == UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED)
212-
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
213-
else
214-
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to write data to TLS context");
216+
switch (errorState.code)
217+
{
218+
case UnityTls.unitytls_error_code.UNITYTLS_SUCCESS:
219+
// In contrast to some other APIs (like Apple security) WOULD_BLOCK is not set if we did a partial write.
220+
// The Mono Api however requires us to set the wantMore flag also if we didn't write all the data.
221+
return (numBytesWritten, numBytesWritten < count);
222+
223+
case UnityTls.unitytls_error_code.UNITYTLS_USER_WOULD_BLOCK:
224+
return (numBytesWritten, true);
225+
226+
case UnityTls.unitytls_error_code.UNITYTLS_STREAM_CLOSED:
227+
return (0, false); // According to Apple and Btls implementation this is how we should handle gracefully closed connections.
215228

216-
return (numBytesWritten, wantMore);
229+
default:
230+
Mono.Unity.Debug.CheckAndThrow (errorState, "Failed to write data to TLS context");
231+
return (0, false);
232+
}
217233
}
218234

219235
public override void Shutdown ()

0 commit comments

Comments
 (0)