classification
Title: Possible issues when remove not completely initialized module from sys.module
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, miss-islington, ncoghlan, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-10-10 10:51 by serhiy.storchaka, last changed 2020-10-14 09:10 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22631 merged serhiy.storchaka, 2020-10-10 10:57
PR 22647 merged miss-islington, 2020-10-11 13:51
PR 22649 closed serhiy.storchaka, 2020-10-11 15:53
Messages (3)
msg378382 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-10 10:51
There are several issues in remove_module() in Python/import.c.

1. PyMapping_HasKey() is used to determine if the module was in sys.modules before removing it. But it can return 0 not only when the key is absent in the mapping, but also when some exceptions was raised during checking (including MemoryError, KeybordInterraption and RecursionError). The corresponding exception will be silenced and the module will be left in sys.modules.

2. If PyMapping_DelItem() fails, a RuntimeError is raised. It is chained with the exception raised in PyMapping_DelItem(), but the original exception, raised before calling remove_module() will be lost.

3. There is a possible race condition between PyMapping_HasKey() and PyMapping_DelItem().
msg378434 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-11 13:51
New changeset 8287aadb75f6bd0154996424819334cd3839707c by Serhiy Storchaka in branch 'master':
bpo-41993: Fix possible issues in remove_module() (GH-22631)
https://github.com/python/cpython/commit/8287aadb75f6bd0154996424819334cd3839707c
msg378608 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-14 09:10
New changeset 391a544f2a52673f6630c672e89840fd6ac36723 by Miss Skeleton (bot) in branch '3.9':
bpo-41993: Fix possible issues in remove_module() (GH-22631) (GH-22647)
https://github.com/python/cpython/commit/391a544f2a52673f6630c672e89840fd6ac36723
History
Date User Action Args
2020-10-14 09:10:13serhiy.storchakasetmessages: + msg378608
2020-10-11 16:21:50serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.8
2020-10-11 15:53:06serhiy.storchakasetpull_requests: + pull_request21623
2020-10-11 13:51:24miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21620
2020-10-11 13:51:18serhiy.storchakasetmessages: + msg378434
2020-10-10 10:57:49serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request21608
2020-10-10 10:51:27serhiy.storchakacreate