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

Exceptions during module initialization are swallowed #1598

Closed
jchampio opened this issue Jul 15, 2023 · 3 comments · Fixed by #1601
Closed

Exceptions during module initialization are swallowed #1598

jchampio opened this issue Jul 15, 2023 · 3 comments · Fixed by #1601

Comments

@jchampio
Copy link
Contributor

Please complete the following information:

  • OS: FreeBSD, macOS
  • Psycopg version: 2.8.6 (but still present in 2.9.6; see below)
  • Python version: 3.11 (incompatible with 2.8.6; see below)
  • PostgreSQL version: 16beta
  • pip version: 23.1.2

Describe the bug

I accidentally installed psycopg2 version 2.8.6 on Python 3.11. This is not supported, but it took me a while to figure out what was wrong because of the following error message:

server/test_oauth.py:19: in <module>
    import psycopg2
../python_venv/lib/python3.11/site-packages/psycopg2/__init__.py:51: in <module>
    from psycopg2._psycopg import (                     # noqa
E   SystemError: initialization of _psycopg raised unreported exception

In this case, initialization of _psycopg raised unreported exception is generated when the module initialization function fails but returns a pointer anyway. It looks like Python expects the module to return NULL on failure instead. If I apply the following patch to 2.8.6:

diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c
index 36fd1f9c..939875e1 100644
--- a/psycopg/psycopgmodule.c
+++ b/psycopg/psycopgmodule.c
@@ -1103,7 +1103,7 @@ INIT_MODULE(_psycopg)(void)
     if (!module) { goto exit; }
 
     if (0 > add_module_constants(module)) { goto exit; }
-    if (0 > add_module_types(module)) { goto exit; }
+    if (0 > add_module_types(module)) { return NULL; }
     if (0 > datetime_init()) { goto exit; }
     if (0 > mxdatetime_init(module)) { goto exit; }
     if (0 > encodings_init(module)) { goto exit; }

then I get a useful message:

server/test_oauth.py:19: in <module>
    import psycopg2
../../../build/python_venv/lib/python3.11/site-packages/psycopg2/__init__.py:51: in <module>
    from psycopg2._psycopg import (                     # noqa
E   SystemError: type psycopg2.extensions.ReplicationConnection has the Py_TPFLAGS_HAVE_GC flag but has no traverse function

which in this case pointed me to the PR for 3.11 support and solved my problem.

Can the INIT_MODULE function be tweaked to always return NULL on failure? That would help with debugging corner cases; it looks like I'm not the only one to run into this [1, 2, 3, 4].

@dvarrazzo
Copy link
Member

dvarrazzo commented Jul 18, 2023 via email

jchampio added a commit to jchampio/psycopg2 that referenced this issue Jul 19, 2023
Previously, any exceptions raised during initialization were swallowed
with a message like

    SystemError: initialization of _psycopg raised unreported exception

Fixes psycopg#1598.
@dvarrazzo
Copy link
Member

Thank you for your contributions, @jchampio :) I've just released psycopg2 2.9.7 to include the two fixes you made.

@jchampio
Copy link
Contributor Author

@dvarrazzo Thank you very much!

pking-moj referenced this issue in ministryofjustice/laa-crime-application-store Jan 15, 2024
Bumps [psycopg2-binary](https://github.com/psycopg/psycopg2) from 2.9.7
to 2.9.9.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/psycopg/psycopg2/blob/master/NEWS">psycopg2-binary's
changelog</a>.</em></p>
<blockquote>
<h2>Current release</h2>
<p>What's new in psycopg 2.9.9
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Add support for Python 3.12.</li>
<li>Drop support for Python 3.6.</li>
</ul>
<p>What's new in psycopg 2.9.8
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Wheel package bundled with PostgreSQL 16 libpq in order to add
support for
recent features, such as <code>sslcertmode</code>.</li>
</ul>
<p>What's new in psycopg 2.9.7
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Fix propagation of exceptions raised during module initialization

(:ticket:<code>[#1598](https://github.com/psycopg/psycopg2/issues/1598)</code>).</li>
<li>Fix building when pg_config returns an empty string
(:ticket:<code>[#1599](https://github.com/psycopg/psycopg2/issues/1599)</code>).</li>
<li>Wheel package bundled with OpenSSL 1.1.1v.</li>
</ul>
<p>What's new in psycopg 2.9.6
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Package manylinux 2014 for aarch64 and ppc64le platforms, in order
to
include libpq 15 in the binary package
(:ticket:<code>[#1396](https://github.com/psycopg/psycopg2/issues/1396)</code>).</li>
<li>Wheel package bundled with OpenSSL 1.1.1t.</li>
</ul>
<p>What's new in psycopg 2.9.5
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Add support for Python 3.11.</li>
<li>Add support for rowcount in MERGE statements in binary packages

(:ticket:<code>[#1497](https://github.com/psycopg/psycopg2/issues/1497)</code>).</li>
<li>Wheel package bundled with OpenSSL 1.1.1r and PostgreSQL 15
libpq.</li>
</ul>
<p>What's new in psycopg 2.9.4
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Fix <code>~psycopg2.extras.register_composite()</code>,
<code>~psycopg2.extras.register_range()</code> with customized
:sql:<code>search_path</code>

(:ticket:<code>[#1487](https://github.com/psycopg/psycopg2/issues/1487)</code>).</li>
<li>Handle correctly composite types with names or in schemas requiring
escape.</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/psycopg/psycopg2/commit/ad5bee7054519d87f25bc5828c502b2ebe197049"><code>ad5bee7</code></a>
chore: bump version number to 2.9.9</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/37d1de1c8f446478666258f4370315f554efa16f"><code>37d1de1</code></a>
chore: add support for Python 3.12</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/abf2723c0a9d89740b433bf49b82ad765e87d5d4"><code>abf2723</code></a>
chore: drop support for Python 3.6</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/2da65a715c71fa5a242fce016835ba500376bf28"><code>2da65a7</code></a>
chore: drop leftover Python 2.7 import aliases from setup.py</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/3fa60fd26839a7e8b083ed3eb3dc592a0f2604f6"><code>3fa60fd</code></a>
chore: bump doc requirement complained by dependabot</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/1c1484e43bdd4927ac5725d4f250b198fbe7600f"><code>1c1484e</code></a>
ci: better interaction with scaleway build server</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/c81cec604f467dfb4fc87c9cbaaf4b758b4ca740"><code>c81cec6</code></a>
chore: bump to next dev release</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/7fe8cb77ca68a06c96d4551c6c9f8cab33904d2a"><code>7fe8cb7</code></a>
chore: bump docs requirements dependabot complains about</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/b39d5d64923a3e321672847bd57c002145f57430"><code>b39d5d6</code></a>
chore: bundle libpq 16</li>
<li><a
href="https://github.com/psycopg/psycopg2/commit/921510d5be7bc2f2dbf7ff3ce613ad0623de79f2"><code>921510d</code></a>
docs: replace &quot;compiled against&quot; with &quot;bundled with&quot;
in news file</li>
<li>Additional commits viewable in <a
href="https://github.com/psycopg/psycopg2/compare/2.9.7...2.9.9">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=psycopg2-binary&package-manager=pip&previous-version=2.9.7&new-version=2.9.9)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants