Skip to content

Commit 543af1b

Browse files
committed
Fix race condition in LiteTask
1 parent d68e378 commit 543af1b

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

Assets/HTC.UnityPlugin/LiteCoroutine/LiteTask.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public LiteTask RestartTask(IEnumerator routine)
101101
{
102102
if (!IsDone) { throw new Exception("Task not done yet."); }
103103
innerRoutine = routine;
104+
current = null;
105+
Exception = null;
104106
state = routine == null ? RunningState.Done : RunningState.Init;
105107
return this;
106108
}
@@ -112,6 +114,8 @@ public LiteTask RestartTask(IEnumerator routine, bool startInBackground)
112114
{
113115
if (!IsDone) { throw new Exception("Task not done yet."); }
114116
innerRoutine = routine;
117+
current = null;
118+
Exception = null;
115119
state = routine == null ? RunningState.Done : RunningState.Init;
116120
StartInBackground = startInBackground;
117121
return this;
@@ -196,30 +200,30 @@ bool IEnumerator.MoveNext()
196200

197201
if (state == RunningState.ToBackground)
198202
{
203+
current = null;
199204
state = RunningState.RunningBackground;
200205
ThreadPool.QueueUserWorkItem(wcBackgroundMoveNextState);
201-
current = null;
202206
return true;
203207
}
204208

205209
return false;
206210
}
207211
}
208212

209-
private bool InnerMoveNext()
213+
private bool InnerMoveNext(out object innerCurrent, out Exception innerException)
210214
{
211215
bool hasNext;
212216
try
213217
{
214218
hasNext = innerRoutine.MoveNext();
215-
current = hasNext ? innerRoutine.Current : null;
216-
Exception = null;
219+
innerCurrent = hasNext ? innerRoutine.Current : null;
220+
innerException = null;
217221
}
218222
catch (Exception ex)
219223
{
220224
hasNext = false;
221-
current = null;
222-
Exception = ex;
225+
innerCurrent = null;
226+
innerException = ex;
223227
}
224228
return hasNext;
225229
}
@@ -231,16 +235,20 @@ private void ForegroundMoveNextState()
231235

232236
private bool ForegroundInnerMoveNext()
233237
{
234-
var hasNext = InnerMoveNext();
238+
bool hasNext;
239+
object innerCurrent;
240+
Exception innerException;
241+
242+
hasNext = InnerMoveNext(out innerCurrent, out innerException);
235243

236244
lock (stateLock)
237245
{
238-
if (Exception != null)
246+
if (innerException != null)
239247
{
248+
Exception = innerException;
240249
state = RunningState.Exception;
241250

242-
if (!Silent) { Debug.LogException(Exception); }
243-
251+
if (!Silent) { Debug.LogException(innerException); }
244252
return false;
245253
}
246254

@@ -255,20 +263,21 @@ private bool ForegroundInnerMoveNext()
255263
return false;
256264
}
257265

258-
if (current != null)
266+
if (innerCurrent != null)
259267
{
260-
if (current == ToBackground)
268+
if (innerCurrent == ToBackground)
261269
{
262270
state = RunningState.ToBackground;
263271
return false;
264272
}
265273

266-
if (current == ToForeground)
274+
if (innerCurrent == ToForeground)
267275
{
268276
return true;
269277
}
270278
}
271279

280+
current = innerCurrent;
272281
state = RunningState.PendingYield;
273282
return false;
274283
}
@@ -283,18 +292,20 @@ private void BackgroundMoveNextState(object o)
283292
private bool BackgroundInnerMoveNext(out TimeSpan outSleepTime)
284293
{
285294
bool hasNext;
295+
object innerCurrent;
296+
Exception innerException;
286297
outSleepTime = TimeSpan.Zero;
287298

288-
hasNext = InnerMoveNext();
299+
hasNext = InnerMoveNext(out innerCurrent, out innerException);
289300

290301
lock (stateLock)
291302
{
292-
if (Exception != null)
303+
if (innerException != null)
293304
{
305+
Exception = innerException;
294306
state = RunningState.Exception;
295307

296-
if (!Silent) { Debug.LogException(Exception); }
297-
308+
if (!Silent) { Debug.LogException(innerException); }
298309
return false;
299310
}
300311

@@ -310,26 +321,27 @@ private bool BackgroundInnerMoveNext(out TimeSpan outSleepTime)
310321
return false;
311322
}
312323

313-
if (current != null)
324+
if (innerCurrent != null)
314325
{
315-
if (current == ToBackground)
326+
if (innerCurrent == ToBackground)
316327
{
317328
return true;
318329
}
319330

320-
if (current == ToForeground)
331+
if (innerCurrent == ToForeground)
321332
{
322333
state = RunningState.ToForeground;
323334
return false;
324335
}
325336

326-
if (current is WaitForTicks)
337+
if (innerCurrent is WaitForTicks)
327338
{
328-
outSleepTime = ((WaitForTicks)current).waitTime;
339+
outSleepTime = ((WaitForTicks)innerCurrent).waitTime;
329340
return true;
330341
}
331342
}
332343

344+
current = innerCurrent;
333345
state = RunningState.PendingYield;
334346
return false;
335347
}

0 commit comments

Comments
 (0)