RejectedSoftware Forums

Sign up

sending messages to self inside receive

Sending messages to self inside receive leads to recursive mutex lock error.

import vibe.d;

shared static this() {
  setLogLevel(LogLevel.debugV);
  setPlainLogging(false);
  auto t1 = runTask({
    yield();
    bool alive = true;
    while(alive) {
      receive(
        (uint){
          logDebug("uint message");
          Task.getThis().send(true); /// fault
        },
        (bool){
          logDebug("bool message");
          alive = false;
        }
      );
    }
    logInfo("t1 terminated");
  });
  
  runTask({
    yield();
    t1.send(23u);
  });
}

Result:

[02240F80:02240E80 dbg] uint message
[02240F80:02240E80 CRITICAL] CoreTaskFiber was terminated unexpectedly: Recursive mutex lock.

Re: sending messages to self inside receive

Fixed now. The only thing that needs to be changes is the order of the message handlers, because a bool is also matched positive against uint:

import vibe.d;

shared static this() {
  setLogLevel(LogLevel.debugV);
  setPlainLogging(false);
  auto t1 = runTask({
    yield();
    bool alive = true;
    while(alive) {
      receive(
        (bool){
          logDebug("bool message");
          alive = false;
        },
        (uint){
          logDebug("uint message");
          Task.getThis().send(true); /// works now
        }
      );
    }
    logInfo("t1 terminated");
  });
 
  runTask({
    yield();
    t1.send(23u);
  });
}

https://github.com/rejectedsoftware/vibe.d/commit/42857a7041db33a0e02e7bd13e35e6a0c13d4105

Re: sending messages to self inside receive

Excellent!