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: C++ Embedded 'time.sleep()' is not working on Windows host due to 'Py_InitializeEx(0)'
Type: behavior Stage: resolved
Components: C API, Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, hafizbilal100, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords: patch

Created on 2020-09-01 12:32 by hafizbilal100, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23344 merged vstinner, 2020-11-17 15:27
PR 23346 merged vstinner, 2020-11-17 17:18
PR 23347 merged vstinner, 2020-11-17 17:26
PR 23349 merged vstinner, 2020-11-17 18:02
PR 23353 merged vstinner, 2020-11-17 21:29
Messages (13)
msg376189 - (view) Author: hafiz bilal (hafizbilal100) Date: 2020-09-01 12:32
Hi,

time.sleep() function is not working on Windows host if I use C API Py_InitializeEx(0) function.

Issue could be reproduce with following simple example.

int main()
{
   Py_InitializeEx(0);
   PyRun_SimpleString("import time \n"
		      "start = time.time() \n"
		      "time.sleep(4) \n"
		      "print('elapsed: %d' % (time.time() - start))");
}

output:

elapsed: 0


Note, issue is not reproduce on Linux host.
msg376196 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-09-01 15:53
The SIGINT event gets created when the _signal extension module is imported. Until then, _PyOS_SigintEvent() returns NULL. But currently all code that calls _PyOS_SigintEvent() assumes it returns a valid handle. This has to be fixed to support Py_InitializeEx(0). 

For example, pysleep could call WinAPI Sleep on the main thread if the interrupt event isn't configured:

        ul_millis = (unsigned long)millisecs;
        hInterruptEvent = _PyOS_SigintEvent();

        if (ul_millis == 0 || !hInterruptEvent || !_PyOS_IsMainThread()) {
            Py_BEGIN_ALLOW_THREADS
            Sleep(ul_millis);
            Py_END_ALLOW_THREADS
            break;
        }
msg376219 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-09-02 09:51
Py_InitializeEx() should be modified to always initialize sigint_event, even if install_sigs is equal to zero.

Maybe the variable should be moved somewhere else. Maybe in the time module?
msg376220 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-09-02 09:52
By the way, it would be nice to add error handling on:

    sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);


See also PR #22049.
msg376232 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-09-02 12:40
> Maybe in the time module?

The SIGINT event is also needed by PyOS_Readline, _io (builtin), _winapi (builtin), and _multiprocessing (pyd). Is the time module guaranteed to be imported in 3.x? It appears to get imported incidentally via _PyImportZip_Init (zipimport). 

Could sigint_event be relocated to a platform-dependent extension of _PyRuntimeState that gets initialized and finalized independent of any module?
msg376265 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2020-09-02 21:09
> Could sigint_event be relocated to a platform-dependent extension of _PyRuntimeState that gets initialized and finalized independent of any module?

This, or leaving it where it is and making sure it gets initialised (it really is about emulating certain signals). Either sounds fine to me.
msg381257 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 17:15
New changeset 0ae323b87f1bed64a7fa70f5a41a5800aca032cc by Victor Stinner in branch 'master':
bpo-41686: Always create the SIGINT event on Windows (GH-23344)
https://github.com/python/cpython/commit/0ae323b87f1bed64a7fa70f5a41a5800aca032cc
msg381265 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 17:57
New changeset cda23be092f4a72e4f335cf182f11e7bd7fd98eb by Victor Stinner in branch 'master':
bpo-41686: Refactor signal_exec() (GH-23346)
https://github.com/python/cpython/commit/cda23be092f4a72e4f335cf182f11e7bd7fd98eb
msg381266 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 17:58
New changeset 05a5d697f4f097f37c5c1e2ed0e2338a33c3fb6a by Victor Stinner in branch '3.9':
bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347)
https://github.com/python/cpython/commit/05a5d697f4f097f37c5c1e2ed0e2338a33c3fb6a
msg381286 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 21:23
New changeset a702bd4b921167e73f8fc987aa64ada571fdc3f8 by Victor Stinner in branch '3.8':
bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) (GH-23349)
https://github.com/python/cpython/commit/a702bd4b921167e73f8fc987aa64ada571fdc3f8
msg381288 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 21:26
Ok, the issue should now be fixed in 3.8, 3.9 and master branches. Thanks for the bug report hafiz bilal.
msg381292 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 21:55
New changeset 29aa624047f893b3b3194f00252b2156bbbf4f9b by Victor Stinner in branch 'master':
bpo-41686: Move _Py_RestoreSignals() to signalmodule.c (GH-23353)
https://github.com/python/cpython/commit/29aa624047f893b3b3194f00252b2156bbbf4f9b
msg381294 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-11-17 21:56
> bpo-41686: Move _Py_RestoreSignals() to signalmodule.c (GH-23353)

Sorry, this change was for bpo-41713.
History
Date User Action Args
2022-04-11 14:59:35adminsetgithub: 85852
2020-11-17 21:56:51vstinnersetmessages: + msg381294
2020-11-17 21:55:39vstinnersetmessages: + msg381292
2020-11-17 21:29:32vstinnersetpull_requests: + pull_request22246
2020-11-17 21:26:11vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg381288

stage: patch review -> resolved
2020-11-17 21:23:25vstinnersetmessages: + msg381286
2020-11-17 18:02:29vstinnersetpull_requests: + pull_request22242
2020-11-17 17:58:17vstinnersetmessages: + msg381266
2020-11-17 17:57:40vstinnersetmessages: + msg381265
2020-11-17 17:26:41vstinnersetpull_requests: + pull_request22239
2020-11-17 17:18:16vstinnersetpull_requests: + pull_request22238
2020-11-17 17:15:29vstinnersetmessages: + msg381257
2020-11-17 15:27:10vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request22235
2020-09-02 21:09:38steve.dowersetmessages: + msg376265
2020-09-02 12:40:41eryksunsetmessages: + msg376232
2020-09-02 12:38:46eryksunsetmessages: - msg376231
2020-09-02 12:36:33eryksunsetmessages: + msg376231
2020-09-02 09:52:26vstinnersetmessages: + msg376220
2020-09-02 09:51:50vstinnersetmessages: + msg376219
2020-09-01 15:56:25vstinnersetnosy: + vstinner
2020-09-01 15:53:25eryksunsetversions: + Python 3.9, Python 3.10
nosy: + paul.moore, tim.golden, eryksun, zach.ware, steve.dower

messages: + msg376196

components: + Windows
type: behavior
2020-09-01 12:32:23hafizbilal100create