This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Exception ignored in: in _shutdown, assert tlock.locked()
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, iritkatriel, martin.panter, pitrou, skydoom, vstinner, zach.ware
Priority: normal Keywords:

Created on 2016-04-05 01:51 by skydoom, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test2.C skydoom, 2016-04-05 01:51 C codes to reproduce the issue
Messages (16)
msg262884 - (view) Author: skydoom (skydoom) Date: 2016-04-05 01:51
I did a search and find the issue 1947, however it's set to "not a bug". In its last note, it's suggested a 'packaging/environment issue'. But since I can reliably reproduce the issue with the "official python package"(that installed by the system, such as I am not building the python from source), Also, the same issue does not occurred on python 2.6.2, but 3.4.3 and 3.5.1. Even though it seems the "AssertionError" message is non-harmful but it's still a bit annoying. I am wondering if you can take a look my issue?

Please compile the attached source codes to reproduce my issue. Note that it only occurred when we (explicitly or implicitly) import the 'threading' module. If we comment out that line, it works fine.
msg262885 - (view) Author: skydoom (skydoom) Date: 2016-04-05 02:01
This is how I compile my code:
CFLAG is obtained from `python3.5m-config --includes`
LDFLAG is obtained from `"python-3.5m-config --ldflags`
g++ ${CFLAG} ${LDFLAG} test2.C 

I am running on Linux.
msg262888 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2016-04-05 03:32
After your `import threading` line, try `print(threading.__file__)`.  The path should be something like `/usr/local/lib/python3.4/threading.py`; if it's not (particularly if the current directory is part of the path), you've found the source of your problem, and you should move/rename the extra threading.py, as suggested by Brandon in msg144004.
msg262889 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-04-05 04:41
I am seeing an AssertionError, but no AttributeError. In your original post you did mention AssertionError, but that contradicts the report title. Please clarify what the problem is.

$ CFLAG="$(python3.5m-config --includes)"
$ LDFLAG="$(python3.5m-config --ldflags)"
$ g++ ${CFLAG} ${LDFLAG} test2.C
$ ./a.out
['/home/proj/python/lib', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-linux', '/usr/lib/python3.5/lib-dynload', '/home/.local/lib/python3.5/site-packages', '/usr/lib/python3.5/site-packages']
Exception ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 1292, in _shutdown
    assert tlock.locked()
AssertionError:
msg262902 - (view) Author: skydoom (skydoom) Date: 2016-04-05 14:06
I print out the threading.__file__ and can see it's "${PYTHONHOME}/lib/python3.5/threading.py" . I don't have any other "threading.py" elsewhere. so what should I do?
msg262903 - (view) Author: skydoom (skydoom) Date: 2016-04-05 14:08
sorry Martin, you are right, my original title was simply (and wrongly) copied over from the issue 1947. You get my identical error message in your post.
msg262905 - (view) Author: skydoom (skydoom) Date: 2016-04-05 14:22
I just update the title to be precise.
Exception ignored in: <module 'threading' from ...threading.py> in _shutdown, assert tlock.locked()
AssertionError:
msg262907 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2016-04-05 15:01
So there is actually no relation to #1947.
msg262913 - (view) Author: skydoom (skydoom) Date: 2016-04-05 15:48
I quote the "issue 1947" just because I did a search in the db before I file this issue and find its output is similar to what I see, (except that it has "AttributeError" which mine does not have.) and because it's filed for 2.5 and I am not sure if this is just some minor "wording changes".

I tend to believe my issue is not the same issue with 1947 now.
msg262997 - (view) Author: skydoom (skydoom) Date: 2016-04-07 17:55
It looks like there is a bug in the _shutdown function of threading.py, that it does not check whether the _main_thread.is_alive() or not.  My temporary fix is to add the following checking in the beginning of _shutdown and it seems no more error message pop up:

if( _main_thread.is_active() is False ):
  return

Please see if this makes sense.
msg262998 - (view) Author: skydoom (skydoom) Date: 2016-04-07 18:02
sorry typo above, is_active should be read as is_alive.
msg263194 - (view) Author: skydoom (skydoom) Date: 2016-04-11 17:55
Hi Pitrou, 

would you be able to look at my issue and the proposed pathc?

Thanks
msg263349 - (view) Author: skydoom (skydoom) Date: 2016-04-13 18:46
seems we also need to check whether _main_thread is daemon thread or not, so the proposed patch would look like:

def _shutdown():
   # add these checking first
   if( _main_thread.isDaemon() is False or _main_thread.is_alive() is False ):
       return
msg355186 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-23 00:06
Extract of test2.C:
---
  // in main thread
  Py_Initialize();
  PyEval_InitThreads();
  PyEval_SaveThread();

  // creating a new sub-interpreter, in my real app, this would be created
  //   in a sub-thread.
  PyInterpreterState* const mainInterpreterState = PyInterpreterState_New();
  PyThreadState* const taskState = PyThreadState_New(mainInterpreterState);
  PyEval_AcquireThread(taskState);
  PyThreadState * const sub_interp = Py_NewInterpreter();
  PyThreadState_Swap( taskState );
  PyEval_ReleaseThread( taskState );
---

IMO this code is not supported. It creates 3 interpreters:

* Py_Initialize(): good
* PyInterpreterState_New(): partially initialized
* Py_NewInterpreter(): created from the partially initialized interpreter

IMHO PyInterpreterState_New() should never be used outside Python internals. Only Py_NewInterpreter() should be used.

At least, this code now fails since PyInterpreterState_New() configuration (interp->config) is empty/uninitizalized, and so _PySys_InitMain() fails.

Moreover, the example uses PyGILState C API which isn't compatible with subinterpreters yet :-(
msg396251 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-21 15:22
Can we close this as not a bug, as the reproducer includes unsupported code?
msg396253 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-21 15:29
Yeah, I consider that the issue can be closed.

I'm ok to investigate the issue if another reproducer code is written only using fully initialized (sub)interpreters.
History
Date User Action Args
2022-04-11 14:58:29adminsetgithub: 70880
2021-06-21 15:33:51iritkatrielsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-06-21 15:29:08vstinnersetmessages: + msg396253
2021-06-21 15:22:15iritkatrielsetnosy: + iritkatriel
messages: + msg396251
2019-10-23 00:06:56vstinnersetnosy: + eric.snow
2019-10-23 00:06:49vstinnersetmessages: + msg355186
2016-04-14 00:58:12vstinnersetnosy: + vstinner
2016-04-13 18:46:40skydoomsetmessages: + msg263349
2016-04-11 17:55:22skydoomsetmessages: + msg263194
2016-04-08 14:10:35skydoomsetnosy: + pitrou
2016-04-07 18:02:52skydoomsetmessages: + msg262998
2016-04-07 17:55:05skydoomsetmessages: + msg262997
2016-04-05 15:48:15skydoomsetmessages: + msg262913
2016-04-05 15:01:29zach.waresetsuperseder: Exception exceptions.AttributeError '_shutdown' in <module 'threading' ->
resolution: duplicate -> (no value)
messages: + msg262907
2016-04-05 14:22:41skydoomsetmessages: + msg262905
title: Exception exceptions.AttributeError '_shutdown' in <module 'threading' > -> Exception ignored in: <module 'threading' from ...threading.py> in _shutdown, assert tlock.locked()
2016-04-05 14:08:51skydoomsetmessages: + msg262903
2016-04-05 14:06:47skydoomsetmessages: + msg262902
2016-04-05 04:41:47martin.pantersetstatus: pending -> open
nosy: + martin.panter
messages: + msg262889

2016-04-05 03:32:44zach.waresetstatus: open -> pending

nosy: + zach.ware
messages: + msg262888

superseder: Exception exceptions.AttributeError '_shutdown' in <module 'threading'
resolution: duplicate
2016-04-05 02:01:01skydoomsetmessages: + msg262885
2016-04-05 01:51:45skydoomcreate