http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51591
--- Comment #2 from Janne Blomqvist <jb at gcc dot gnu.org> 2011-12-17 11:27:40 UTC --- Looks like some kind of race condition.. E.g. what about: STOP calls exit(), which leads to the library destructor being called, which calls close_units(), which closes each open unit in the tree. But somehow the print statement from another thread also thinks it has access to the unit, and then tries to print something, which segfaults because the other thread is in the process of shutting down the same unit? Hmm, now that I quickly looked at the code, the above looks likely. So close_units() acquires unit_lock (the global lock protecting the unit tree), then closes each unit without acquiring the unit's own lock (u->lock). For comparison, in normal IO statements, first we acquire unit_lock, find the unit in the tree, acquire u->lock, then release unit_lock. Then do the IO with u->lock held, and finally relase u->lock. So it seems that it would be possible for the print statement to acquire the u->lock before the close_units gets to lock unit_lock, and thus we have a race? Of course, this is based on a very quick scan of the code, and I could be all wrong. Perhaps Jakub knows better, as he designed the libgfortran locking scheme?