Title: [2.7] bpo-34110: Fix module load problem of cPickle in case of multithreading by sangongs · Pull Request #8276 · python/cpython · GitHub
Open Graph Title: [2.7] bpo-34110: Fix module load problem of cPickle in case of multithreading by sangongs · Pull Request #8276 · python/cpython
X Title: [2.7] bpo-34110: Fix module load problem of cPickle in case of multithreading by sangongs · Pull Request #8276 · python/cpython
Description: In Python 2.7, sys.modules may contain a partially loaded module. This is caused by the following logic: cpython/Python/import.c Lines 721 to 727 in a45fa39 PyObject * PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) { PyObject *modules = PyImport_GetModuleDict(); PyObject *m, *d, *v; m = PyImport_AddModule(name); When executing the code of a module, an empty module is added into the module dict. Thus if any code in the module releases the GIL, another thread may get a partially loaded module from sys.modules. Unfortunately, cPickle checks sys.modules before trying to import and may get an incomplete module, leading to an AttributeError when trying to obtain an object of the module. This fix acquires import lock before checking sys.modules, make sure there is no partially loaded module. To reproduce, first create a foo.py: import time time.sleep(0.1) class foo(): pass Then create and run main.py: import threading import cPickle threads = [threading.Thread(target=cPickle.loads, args=('cfoo\nfoo\np0\n.',)) for _ in range(2)] [thread.start() for thread in threads] [thread.join() for thread in threads] The following error should be raised: Exception in thread Thread-2: Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) AttributeError: 'module' object has no attribute 'foo' https://bugs.python.org/issue34110
Open Graph Description: In Python 2.7, sys.modules may contain a partially loaded module. This is caused by the following logic: cpython/Python/import.c Lines 721 to 727 in a45...
X Description: In Python 2.7, sys.modules may contain a partially loaded module. This is caused by the following logic: cpython/Python/import.c Lines 721 to 727 in a45...
Opengraph URL: https://github.com/python/cpython/pull/8276
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:bbd19afc-ca60-0ff9-1ddb-91cb51c3cbbb |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A538:203A5F:2D10F22:3D7C33A:6A5457FA |
| html-safe-nonce | 96da537b94088716567d360f0da7eae6dec551a058411771d169219c18fd368b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTM4OjIwM0E1RjoyRDEwRjIyOjNEN0MzM0E6NkE1NDU3RkEiLCJ2aXNpdG9yX2lkIjoiODM2OTAzOTE2ODcxMDY2MjEzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3973b5db7fdbc77c1ccd46b933e7acdeded27efccc234678838f7a1c68b55de2 |
| hovercard-subject-tag | pull_request:201373550 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/8276/files |
| twitter:image | https://avatars.githubusercontent.com/u/11306497?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/11306497?s=400&v=4 |
| og:image:alt | In Python 2.7, sys.modules may contain a partially loaded module. This is caused by the following logic: cpython/Python/import.c Lines 721 to 727 in a45... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6d28624802c2f7d9500239dd6870ed7031b7353b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width