Skip to content

Py_AddPendingCall Inserted Function Never Called in 3.8, works in 3.6 #87648

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

Closed
ChrisMorton mannequin opened this issue Mar 12, 2021 · 3 comments
Closed

Py_AddPendingCall Inserted Function Never Called in 3.8, works in 3.6 #87648

ChrisMorton mannequin opened this issue Mar 12, 2021 · 3 comments
Labels
3.8 (EOL) end of life topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@ChrisMorton
Copy link
Mannequin

ChrisMorton mannequin commented Mar 12, 2021

BPO 43482
Nosy @chris Morton

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 = None
created_at = <Date 2021-03-12.22:11:40.187>
labels = ['expert-C-API', 'type-bug', '3.8']
title = 'Py_AddPendingCall Inserted Function Never Called in 3.8, works in 3.6'
updated_at = <Date 2021-03-15.03:29:17.721>
user = 'https://github.com/ChrisMorton'

bugs.python.org fields:

activity = <Date 2021-03-15.03:29:17.721>
actor = 'chrisgmorton'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['C API']
creation = <Date 2021-03-12.22:11:40.187>
creator = 'chrisgmorton'
dependencies = []
files = []
hgrepos = []
issue_num = 43482
keywords = []
message_count = 2.0
messages = ['388559', '388704']
nosy_count = 1.0
nosy_names = ['chrisgmorton']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43482'
versions = ['Python 3.8']

@ChrisMorton
Copy link
Mannequin Author

ChrisMorton mannequin commented Mar 12, 2021

Building code on Mac OSX or Linux Ubuntu 16.04

#include <Python.h>
#include <thread>
#include <chrono>
#include <iostream>

// MacOSX build:
// g++ stop.cpp -I <prod root dir>/include/pythonX.X -L <prod root dir>/lib -lpythonX.X -o stop
// Linuxe requires addtional linkage:
// -lpthread

void RunPythonScript()
{
    PyRun_SimpleString("# import sys \n"
                       "import time \n"
                       "# sys.setcheckinterval(-1) \n"
                       "while True: \n"
                       "    print('Running!') \n"
                       "    time.sleep(1) \n"
                       );
std::cout \<\< "Terminating Python Interpreter." \<\< std::endl;

}

int Stop(void *)
{
    std::cout << "We threw an exception." <<std::endl;    
    PyErr_SetInterrupt();
    PyErr_SetString(PyExc_RuntimeError, "Stop Python Execution.");
    return -1;

}

int main (int argc, char *argv[])
{

Py_Initialize(); // The Python Interpreter must be created in the main thread.

std::thread th(&RunPythonScript);    // Now execute our python scipt in a thread.

std::cout << "Python has started!" << std::endl;    
std::this_thread::sleep_for(std::chrono::seconds(5));

std::cout << "Asking Python to exit." << std::endl;
    PyGILState_STATE state = PyGILState_Ensure();    
    int r = Py_AddPendingCall(&Stop, NULL); // inject our Stop routine
    std::cout << r <<std::endl;
    PyGILState_Release(state);
    
    th.join(); // Wait for the thread to catch up. 

    Py_Finalize();

    std::cout << "Exiting Main Function." << std::endl;
    
    std::this_thread::sleep_for(std::chrono::seconds(2));
    
return 0;
}

Linking against 3.6 and executing leads to the successful interruption of Python code execution:

Python has started!
Running!
Running!
Running!
Running!
Running!
Asking Python to exit.
0
We threw an exception.
Traceback (most recent call last):
  File "<string>", line 6, in <module>
RuntimeError: Stop Python Execution.
Terminating Python Interpreter.
Exiting Main Function.

The Stop function is never called with the same code linked against Python 3.8.

@ChrisMorton ChrisMorton mannequin added 3.8 (EOL) end of life topic-C-API labels Mar 12, 2021
@ChrisMorton
Copy link
Mannequin Author

ChrisMorton mannequin commented Mar 15, 2021

I should add that Py_AddPendingCall returns 0 in both cases.

@ChrisMorton ChrisMorton mannequin changed the title PyAddPendingCall Function Never Called in 3.8, works in 3.6 Py_AddPendingCall Function Never Called in 3.8, works in 3.6 Mar 15, 2021
@ChrisMorton ChrisMorton mannequin added the type-bug An unexpected behavior, bug, or error label Mar 15, 2021
@ChrisMorton ChrisMorton mannequin changed the title PyAddPendingCall Function Never Called in 3.8, works in 3.6 Py_AddPendingCall Function Never Called in 3.8, works in 3.6 Mar 15, 2021
@ChrisMorton ChrisMorton mannequin added the type-bug An unexpected behavior, bug, or error label Mar 15, 2021
@ChrisMorton ChrisMorton mannequin changed the title Py_AddPendingCall Function Never Called in 3.8, works in 3.6 Py_AddPendingCall Inserted Function Never Called in 3.8, works in 3.6 Mar 15, 2021
@ChrisMorton ChrisMorton mannequin changed the title Py_AddPendingCall Function Never Called in 3.8, works in 3.6 Py_AddPendingCall Inserted Function Never Called in 3.8, works in 3.6 Mar 15, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@kumaraditya303
Copy link
Contributor

Duplicate of #95820

@kumaraditya303 kumaraditya303 marked this as a duplicate of #95820 Feb 13, 2023
@kumaraditya303 kumaraditya303 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant