Skip to content

Commit 2604ec8

Browse files
committed
Race condition workaround
googlesamples#599
1 parent 8cbe65f commit 2604ec8

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

Diff for: source/AndroidResolver/src/CommandLine.cs

+24-13
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,32 @@ private void Read()
213213
stream.BeginRead(
214214
buffer, 0, buffer.Length, (asyncResult) => {
215215
int bytesRead = stream.EndRead(asyncResult);
216-
if (!complete)
217-
{
218-
complete = bytesRead == 0;
219-
if (DataReceived != null)
220-
{
221-
byte[] copy = new byte[bytesRead];
222-
Array.Copy(buffer, copy, copy.Length);
223-
DataReceived(new StreamData(
224-
handle, Encoding.UTF8.GetString(copy), copy,
225-
complete));
226-
}
227-
}
216+
BytesReadCallback(bytesRead);
228217
readEvent.Set();
229218
}, null);
230-
readEvent.WaitOne();
219+
var waitResult = readEvent.WaitOne(10000);
220+
221+
if (!waitResult)
222+
{
223+
UnityEngine.Debug.Log($"Timeout of {nameof(AsyncStreamReader)} waiting");
224+
BytesReadCallback(0);
225+
}
226+
}
227+
228+
void BytesReadCallback(int bytesRead)
229+
{
230+
if (complete)
231+
return;
232+
233+
complete = bytesRead == 0;
234+
if (DataReceived == null)
235+
return;
236+
237+
byte[] copy = new byte[bytesRead];
238+
Array.Copy(buffer, copy, copy.Length);
239+
DataReceived(new StreamData(
240+
handle, Encoding.UTF8.GetString(copy), copy,
241+
complete));
231242
}
232243
}
233244

0 commit comments

Comments
 (0)