<div dir="ltr">some weird bugs can occur when, for example, an error or signal handler is re-entrant.<div>Is there a way to detect this?</div><div><br></div><div>this works but is cumbersome to use [see below], is there a simpler way to detect in calling code (i want to simplify SNIPPET in calling code)</div><div><br></div><div>---</div><div><div>module util.reentrance_check;</div><div>import std.stdio;</div><div><br></div><div>void reentrance_check(ref int depth, string file=__FILE__, int line=__LINE__){</div><div>  depth++;</div><div>  if(depth>1){</div><div>    // make sure code here is not reentrant</div><div>    writeln(file, ":", line, " reentrant ", depth, " ");</div><div>    // print stack trace...</div><div>    import core.stdc.stdlib: exit;</div><div>    exit(1);// because assert(0) could in turn call a handler which could re-enter here</div><div>    return;</div><div>  }</div><div>}</div><div><br></div><div>unittest{</div><div>  example_reentrance_check;</div><div>  example_reentrance_check;</div><div>}</div><div><br></div><div>// USAGE example</div><div>void example_reentrance_check(){</div><div>  // NOTE: can't use RAII because $depth a static var</div><div>  // TODO: how can we simplify this, to a 1-liner?</div><div><br></div><div>   // insert this block in your non-re-entrant function</div><div>  import util.reentrance_check;</div><div>  static int depth=0;</div><div>  reentrance_check(depth);</div><div>  scope(success) depth--;</div><div><br></div><div>  /// code after</div><div>  version(bad) example_reentrance_check;// should crash with -version=bad</div><div>}</div><div><br></div></div><div>---</div><div><br></div></div>