Skip to content

Commit

Permalink
Wait for stop in RedisSubscriberImpl.~this(). Closes #1201.
Browse files Browse the repository at this point in the history
This is an updated version of #1201.
  • Loading branch information
Etienne Cimon authored and s-ludwig committed Nov 5, 2017
1 parent 2bdce62 commit a800f94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
33 changes: 32 additions & 1 deletion redis/vibe/db/redis/redis.d
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ final class RedisSubscriberImpl {
Task m_listener;
Task m_listenerHelper;
Task m_waiter;
Task m_stopWaiter;
InterruptibleRecursiveTaskMutex m_mutex;
InterruptibleTaskMutex m_connMutex;
}
Expand Down Expand Up @@ -684,9 +685,37 @@ final class RedisSubscriberImpl {
m_connMutex = new InterruptibleTaskMutex;
}

// FIXME: instead of waiting here, the class must be reference counted
// and destructions needs to be defered until everything is stopped
~this() {
logTrace("~this");
bstop();
waitForStop();
}

// Task will block until the listener is finished
private void waitForStop()
{
logTrace("waitForStop");
if (!m_listening) return;

void impl() @safe {
m_mutex.performLocked!({
m_stopWaiter = Task.getThis();
});
if (!m_listening) return; // verify again in case the mutex was locked by bstop
scope(exit) {
m_mutex.performLocked!({
m_stopWaiter = Task();
});
}
bool stopped;
do {
() @trusted { receive((Action act) { if (act == Action.STOP) stopped = true; }); } ();
} while (!stopped);

enforce(stopped, "Failed to wait for Redis listener to stop");
}
inTask(&impl);
}

/// Stop listening and yield until the operation is complete.
Expand Down Expand Up @@ -1112,6 +1141,8 @@ final class RedisSubscriberImpl {

if (m_waiter != Task())
() @trusted { m_waiter.send(Action.STOP); } ();
if (m_stopWaiter != Task())
() @trusted { m_stopWaiter.send(Action.STOP); } ();

m_listenerHelper = Task();
m_listener = Task();
Expand Down
3 changes: 2 additions & 1 deletion tests/redis/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void runTest()
{
//setLogLevel(LogLevel.trace);
/* open a redis server locally to run these tests
* Windows download link: https://github.com/MSOpenTech/redis/tree/2.8/bin/release
* Windows download link: https://github.com/MSOpenTech/redis/releases
* Linux: use "yum install redis" on RHEL or "apt-get install redis" on Debian-like
*/
RedisClient redis;
Expand Down Expand Up @@ -96,6 +96,7 @@ void runTest()
{
RedisSubscriber scoped = redis.createSubscriber();
sub = scoped;
sleep(50.msecs);
}
import std.datetime;

Expand Down

0 comments on commit a800f94

Please sign in to comment.