Fix bugs in thread termination causing occasional pthread
authorCarl Hetherington <cth@carlh.net>
Wed, 29 Jul 2020 18:22:54 +0000 (20:22 +0200)
committerCarl Hetherington <cth@carlh.net>
Wed, 29 Jul 2020 18:22:54 +0000 (20:22 +0200)
commite3c7656f9dc0acbaf518c051b847ee2e4eb7ba23
tree311f7ad01d1ef264778aebb2ad9b844c8015d068
parent09860271bb6d03b3937c08bffb4c672697f6d711
Fix bugs in thread termination causing occasional pthread
assertion failures.

Before this, it was possible for J2KEncoder::terminate_threads()
to finish without terminating all threads if the thread _running_
terminate_threads() was itself interrupt()ed.

This is because the thread_group::join_all() in terminate_threads()
is an interruption point, so it was possible it not to complete
but instead to throw interrupted_exception.  Then the owning
J2KEncoder would be torn down but the threads would still be running,
causing use-after-frees.

This commit adds some boost::this_thread::disable_interruption
objects to ensure that the owning thread is not interrupted while
it is being destroyed.

Also tidy up code that does this stuff, assuming that it's safe
to not call thread::joinable but instead do

thread.interrupt();
try {
  thread.join();
} catch (...) {}
12 files changed:
src/lib/butler.cc
src/lib/checker.cc
src/lib/encode_server.cc
src/lib/encode_server_finder.cc
src/lib/hints.cc
src/lib/j2k_encoder.cc
src/lib/job.cc
src/lib/job_manager.cc
src/lib/update_checker.cc
src/lib/writer.cc
src/wx/gl_video_view.cc
test/socket_test.cc