Skip to content

Commit

Permalink
Fix TaskPipeImpl.waitForData with timeout to actually time out. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Dec 19, 2016
1 parent 96e4ac0 commit 4d0e890
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions source/vibe/stream/taskpipe.d
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,20 @@ private final class TaskPipeImpl {

/** Blocks until at least one byte of data has been written to the pipe.
*/
void waitForData(Duration timeout = 0.seconds)
void waitForData(Duration timeout = Duration.max)
{
import std.datetime : Clock, SysTime, UTC;
bool have_timeout = timeout > 0.seconds && timeout != Duration.max;
SysTime now = Clock.currTime(UTC());
SysTime timeout_target;
if (have_timeout) timeout_target = now + timeout;

synchronized (m_mutex) {
while (m_buffer.empty && !m_closed) {
if (timeout > 0.seconds)
m_condition.wait(timeout);
while (m_buffer.empty && !m_closed && (!have_timeout || now < timeout_target)) {
if (have_timeout)
m_condition.wait(timeout_target - now);
else m_condition.wait();
now = Clock.currTime(UTC());
}
}
}
Expand Down Expand Up @@ -201,3 +208,16 @@ unittest { // issue #1501 - deadlock in TaskPipe
joiner.join();
}
}

unittest { // issue #
auto t = runTask({
auto tp = new TaskPipeImpl;
tp.waitForData(10.msecs);
exitEventLoop();
});
runTask({
sleep(500.msecs);
assert(!t.running, "TaskPipeImpl.waitForData didn't timeout.");
});
runEventLoop();
}

0 comments on commit 4d0e890

Please sign in to comment.