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: [VS2013] Py_InitializeEx causes fatal error being called from winnt-service
Type: crash Stage:
Components: Interpreter Core, IO, Windows Versions: Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Visual C++ 11.0 reports fileno(stdin) == 0 for non-console program
View: 17797
Assigned To: Nosy List: paul.moore, steve.dower, tim.golden, vmurashev, zach.ware
Priority: normal Keywords: patch

Created on 2015-07-03 17:47 by vmurashev, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pythonrun.c.diff vmurashev, 2015-07-03 18:04
Messages (4)
msg246199 - (view) Author: Vitaly Murashev (vmurashev) * Date: 2015-07-03 17:47
[Affects Windows only]
Brief description (after analysis in debugger):

Py_InitializeEx fails inside internal call:

1.
    if (initstdio() < 0)
        Py_FatalError(
            "Py_Initialize: can't initialize sys standard streams");

2. inside initstdio():

    if (!is_valid_fd(fd)) {
        std = Py_None;
        Py_INCREF(std);
    }
    else {
// ===> is_valid_fd() passed and we come here
        std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
        if (std == NULL)
            goto error; // ===> this goto leads to fatal error

3.
is_valid_fd(int fd)   /// => JFI: fd=0
{
    int dummy_fd;
    if (fd < 0 || !_PyVerify_fd(fd))
        return 0;
    dummy_fd = dup(fd); /// ==>> dup() WORKS well
    if (dummy_fd < 0)
        return 0;
    close(dummy_fd);
    return 1;
}

4.
Let's Look whats going in create_stdio():
Modules\_io\fileio.c

static int
fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
{ ...
    if (fd >= 0) {
        if (check_fd(fd))
            goto error;    /// => fail is here 

5. Let's have a look at check_fd():
static int
check_fd(int fd)
{
#if defined(HAVE_FSTAT) /// => yes, it is defined for Windows
    struct stat buf;
    if (!_PyVerify_fd(fd) || (fstat(fd, &buf) < 0 && errno == EBADF)) {
        PyObject *exc;
        char *msg = strerror(EBADF);
        exc = PyObject_CallFunction(PyExc_OSError, "(is)",
                                    EBADF, msg);
        PyErr_SetObject(PyExc_OSError, exc);
        Py_XDECREF(exc);
        return -1;
    }
#endif
    return 0;
}
msg246201 - (view) Author: Vitaly Murashev (vmurashev) * Date: 2015-07-03 17:58
More details:
previously Python3.4.3 was compiled in my environment using compiler from VisualStudio-2005 and everything worked well. The crash has come right after changing compiler to the one from VisualStudio-2013

So something definitely changed inside Microsoft runtime implementation.
For unknown reason when winnt-service is running, 
its std file handles 0,1,2 can be duplicated using dup()
but fstat() fails
msg246204 - (view) Author: Vitaly Murashev (vmurashev) * Date: 2015-07-03 18:04
patch suggested
msg246206 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2015-07-03 19:10
See #17797
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68749
2015-07-03 19:10:38steve.dowersetstatus: open -> closed
superseder: Visual C++ 11.0 reports fileno(stdin) == 0 for non-console program
resolution: duplicate
messages: + msg246206
2015-07-03 18:06:51vmurashevsettitle: [VS2013] Py_InitializeEx causes fatal error being from winnt-service -> [VS2013] Py_InitializeEx causes fatal error being called from winnt-service
2015-07-03 18:04:40vmurashevsetfiles: + pythonrun.c.diff
keywords: + patch
messages: + msg246204
2015-07-03 17:58:29vmurashevsetmessages: + msg246201
2015-07-03 17:47:45vmurashevcreate