text_combined: guido van rossum hoaufpythonorg something keeps awake night aware solution counter seems fine except think perhaps close call raise ioerror instead set flag thread makes counter go zero close thread file got closed used course concurrency issues besides close two threads try io file c stdio library case stdio files threadsafe c level classically contradicting think problem perhaps io operation disallowed file use another thread guido mon mar 31 2008 109 pm antoine pitrou wrote hello seems subject quite bit history tim peters demonstrated problem 2003 message httpmailpythonorgpipermailpythondev2003june036537html short python file objects release gil calling c stdlib function embedded file pointer unfortunately another thread calls fclose file pointer concurrently contents pointed become garbage interpreter process crashes using file object two threads running pure python code crash interpreter another easiertosolve problem file pointer stored file object could become null point used another thread problem could store file pointer local variable releasing gil et voilà discussion time possible resolution ive tried fix problem ive come think satisfying solution sum following bullet points file object gets dedicated counter incremented bject releases gil decremented gil taken thus counter keeps track many running unlocked sections code using particular file object please note counter doesnt need lock since modified gilprotected sections close method aforementioned counter greater 0 refuse call fclose instead raise ioerror may seem like worrying semantic change dont think following reasons 1 closed file pointer anyway interpreter would likely crash another thread would using garbage data thats trying fix 2 close raises ioerror called later worse fclose called file object garbage collected 3 close already raise ioerror fclose fails whatever reason although sure probably rare 4 doesnt seem wrong notify programmer code unsafe patch attached httpbugspythonorgissue815646 addresses least hope potential problems pure python code threads file object doesnt try fix c extensions using pyfile_asfile api dirty things file pointer could second step approach accepted noted 2003 discussions would probably involve new api whether want introduce api python 2x python 30 different io model anyway left open discussion regards antoine _______________________________________________ pythondev mailing list zvlllneumpythonorg httpmailpythonorgmailmanlistinfopythondev unsubscribe httpmailpythonorgmailmanoptionspythondevguido40pythonorg guido van rossum home page httpwwwpythonorgguido _______________________________________________ pythondev mailing list zvlllneumpythonorg httpmailpythonorgmailmanlistinfopythondev unsubscribe httpmailpythonorgmailmanoptionspythondevpythondev40tangomucom wed 06 aug 2008 215630 0700 pythondev threadsafe file objects return