Skip to content

Commit

Permalink
python: Add a missing Py_INCREF() in nxt_python_start()
Browse files Browse the repository at this point in the history
On some Python 3.11 systems, 3.11.9 & 3.11.10, we were seeing a crash
triggered by Py_Finalize() in nxt_python_start() when running one of our
pytests, namely
test/test_python_factory.py::test_python_factory_invalid_callable_value

  2024/09/12 15:07:29 [alert] 5452#5452 factory "wsgi_invalid_callable" in module "wsgi" can not be called to fetch callable
  Fatal Python error: none_dealloc: deallocating None: bug likely caused by a refcount error in a C extension
  Python runtime state: finalizing (tstate=0x00007f560b88a718)

  Current thread 0x00007f560bde7ad0 (most recent call first):
    <no Python frame>
  2024/09/12 15:07:29 [alert] 5451#5451 app process 5452 exited on signal 6 (core dumped)

It seems this was caused by

  Py_XDECREF(nxt_py_stderr_flush);

in nxt_python_atexit().

We never explicitly INCREF() this variable (though there are plenty of
other cases of doing DECREF()'s with no previous INCREF()'s). However in
this case adding an INCREF() for this variable does fix the crash.

I'm not sure we really need to reference count this variable, it's only
used in nxt_python_print_exception(), but it's perhaps the safest course
of action.

Cc: Konstantin Pavlov <[email protected]>
Closes: #1413
Signed-off-by: Andrew Clayton <[email protected]>
  • Loading branch information
ac000 committed Sep 12, 2024
1 parent 5fde2ff commit c9860dc
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/python/nxt_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ nxt_python_start(nxt_task_t *task, nxt_process_data_t *data)
goto fail;
}

Py_INCREF(nxt_py_stderr_flush);

if (nxt_slow_path(nxt_python_set_path(task, c->path) != NXT_OK)) {
goto fail;
}
Expand Down

0 comments on commit c9860dc

Please sign in to comment.