Skip to content
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

C++ Embedded 'time.sleep()' is not working on Windows host due to 'Py_InitializeEx(0)' #85852

Closed
hafizbilal100 mannequin opened this issue Sep 1, 2020 · 13 comments
Closed
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes OS-windows topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@hafizbilal100
Copy link
Mannequin

hafizbilal100 mannequin commented Sep 1, 2020

BPO 41686
Nosy @pfmoore, @vstinner, @tjguk, @zware, @eryksun, @zooba
PRs
  • bpo-41686: Always create the SIGINT event on Windows #23344
  • bpo-41686: Refactor signal_exec() #23346
  • [3.9] bpo-41686: Always create the SIGINT event on Windows (GH-23344) #23347
  • [3.8] bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) #23349
  • bpo-41686: Move _Py_RestoreSignals() to signalmodule.c #23353
  • 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:

    assignee = None
    closed_at = <Date 2020-11-17.21:26:11.457>
    created_at = <Date 2020-09-01.12:32:23.361>
    labels = ['type-bug', '3.8', '3.9', '3.10', 'expert-C-API', 'OS-windows']
    title = "C++ Embedded 'time.sleep()' is not working on Windows host due to 'Py_InitializeEx(0)'"
    updated_at = <Date 2020-11-17.21:56:51.779>
    user = 'https://bugs.python.org/hafizbilal100'

    bugs.python.org fields:

    activity = <Date 2020-11-17.21:56:51.779>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-17.21:26:11.457>
    closer = 'vstinner'
    components = ['Windows', 'C API']
    creation = <Date 2020-09-01.12:32:23.361>
    creator = 'hafizbilal100'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41686
    keywords = ['patch']
    message_count = 13.0
    messages = ['376189', '376196', '376219', '376220', '376232', '376265', '381257', '381265', '381266', '381286', '381288', '381292', '381294']
    nosy_count = 7.0
    nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'hafizbilal100']
    pr_nums = ['23344', '23346', '23347', '23349', '23353']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41686'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @hafizbilal100
    Copy link
    Mannequin Author

    hafizbilal100 mannequin commented Sep 1, 2020

    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.

    @hafizbilal100 hafizbilal100 mannequin added 3.8 only security fixes topic-C-API labels Sep 1, 2020
    @eryksun
    Copy link
    Contributor

    eryksun commented Sep 1, 2020

    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;
            }

    @eryksun eryksun added OS-windows 3.9 only security fixes 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Sep 1, 2020
    @vstinner
    Copy link
    Member

    vstinner commented Sep 2, 2020

    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?

    @vstinner
    Copy link
    Member

    vstinner commented Sep 2, 2020

    By the way, it would be nice to add error handling on:

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

    See also PR bpo-22049.

    @eryksun
    Copy link
    Contributor

    eryksun commented Sep 2, 2020

    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?

    @zooba
    Copy link
    Member

    zooba commented Sep 2, 2020

    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.

    @vstinner
    Copy link
    Member

    New changeset 0ae323b by Victor Stinner in branch 'master':
    bpo-41686: Always create the SIGINT event on Windows (GH-23344)
    0ae323b

    @vstinner
    Copy link
    Member

    New changeset cda23be by Victor Stinner in branch 'master':
    bpo-41686: Refactor signal_exec() (GH-23346)
    cda23be

    @vstinner
    Copy link
    Member

    New changeset 05a5d69 by Victor Stinner in branch '3.9':
    bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347)
    05a5d69

    @vstinner
    Copy link
    Member

    New changeset a702bd4 by Victor Stinner in branch '3.8':
    bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) (GH-23349)
    a702bd4

    @vstinner
    Copy link
    Member

    Ok, the issue should now be fixed in 3.8, 3.9 and master branches. Thanks for the bug report hafiz bilal.

    @vstinner
    Copy link
    Member

    New changeset 29aa624 by Victor Stinner in branch 'master':
    bpo-41686: Move _Py_RestoreSignals() to signalmodule.c (GH-23353)
    29aa624

    @vstinner
    Copy link
    Member

    bpo-41686: Move _Py_RestoreSignals() to signalmodule.c (GH-23353)

    Sorry, this change was for bpo-41713.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes OS-windows topic-C-API type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants