Menu

#176 TXBase::InstanceCount is not thread-safe

6.40
closed
5
2014-04-26
2011-12-12
No

TXBase::InstanceCount is intended to keep a count of the number of TXBase instances in the process. Since access to this static variable is not synchronized, its value is unreliable in a multi-threaded program.

Related

Bugs: #376

Discussion

  • Vidar Hasfjord

    Vidar Hasfjord - 2011-12-12

    Also, the intended use of the variable seems to be to detect that exceptions are in flight, but this is unreliable in itself because a program can instantiate TXBase without throwing it immediately (or ever), e.g. to store the current exception in flight and rethrow another (convert/upgrade exceptions). Also, a program can throw exceptions not derived from TXBase. There are other better ways of detecting whether an exception is in flight (see std::uncaught_exception, but note undefined behaviour in multi-threaded programs).

    InstanceCount is used in only one place in OWLNext; TXRegistry::Check use it seemingly for the purpose of detecting if exceptions are in flight. This seems ill-adviced. If InstanceCount > 0, despite the 'stat' argument indicating an error condition, the Check function will just return, and program-flow continues with possibly unexpected results. An exception should always be thrown in Check, even if this leads to program shutdown during stack unwinding. A safe program should use exception handling to ensure that exceptions are handled during stack unwinding (i.e. in destructors). Ignoring error conditions is not a solution.

    For these reasons I propose that we simply remove TXBase::InstanceCount and make TXRegistry::Check always throw on errors.

     

    Last edit: Vidar Hasfjord 2012-09-28
  • Vidar Hasfjord

    Vidar Hasfjord - 2014-04-26
    • Status: pending --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB