New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return code of pthread_* in thread_pthread.h is not used for perror #74045
Comments
Python/thread_pthread.h:145 defines the CHECK_STATUS macro used for printing error messages in case any of the calls fail. CHECK_STATUS uses perror for formatting an error message, which relies on the global erno being set (see man perror). Since the pthread functions return their status code instead of setting erno (which might not even work in threaded environments), no additional information is displayed. See for example produced by PyThread_release_lock: pthread_mutex_lock[3]: Undefined error: 0 The correct solution would be to use strerror(status) in order to show the proper message. |
I have attached a diff adding a new macro for handling pthread_* status codes. Will submit PR as soon as my CLA is approved. |
Could you give us minimum sample Python code to reproduce the error? |
While you might scold me for the following code, it is just some fiddling with the cpython functions from python side. Backstory is bpo-6721 or related to it. I am working on a multi-process service which uses multiprocessing (or rather the billiard fork). While forking I run into deadlocks as described in the issue when I fork while the io lock is acquired. In order to find a solution I experimented with the cpython module and tried to release the lock manually after a fork. The code can be found attached (tested with python 3.6/3.7 on OSX Sierra). While it does not really do what it is supposed to do, it shows the problem described in this issue (bpo-29859): If the internal calls to the pthread_* functions fail, I will only get a generic error message: pthread_mutex_lock[3]: Undefined error: 0 In reality the produced error messages should be: pthread_mutex_lock[3]: Invalid argument This happens because the pthread_* functions do not set the erno variable as described in the issue description. Please note that the issue is not only related to my code, but might affect all code using locks. While I suppose there shouldn't be any errors when calling the low level functions, the attached patch/pr will make debugging a lot easier when it actually happens by displaying the correct error message. |
I don't know your patch is worth enough or not. (I dislike fprintf(stderr, ...) at least). But my advice is stop mixing multithreading and fork (except fork+exec). While Python has GIL, some extension can release GIL and run any C code. For more information, see "rational" section in |
While I agree, fprintf it not really nice, I looked through other parts of the python source where information is printed to stderr and fprintf was used there as well, so I fell back to it myself. % grep -rnw . -e "fprintf(stderr," | wc -l 178 Using threading and multiprocessing is insane, I know that. Nevertheless the error codes returned by the pthread_* calls are processed incorrectly, so I would consider this a bug worth fixing. |
OK, perror() writes to stderr too. fair enough. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: