Skip to content

Using psycopg2 connection_factory throws exceptions #402

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
mjgp2 opened this issue Sep 6, 2023 · 4 comments
Closed

Using psycopg2 connection_factory throws exceptions #402

mjgp2 opened this issue Sep 6, 2023 · 4 comments

Comments

@mjgp2
Copy link

mjgp2 commented Sep 6, 2023

If using a connection factory, the connections are double wrapped and exceptions are thrown from psycopg2 type checking, e.g. TypeError: argument 2 must be a connection, cursor or None

e.g.

ThreadedConnectionPool(
    min_connections,
    max_connections,
    connection_string,
    connection_factory=iam_connection_factory(connection_string) if use_iam else None,
)

The workaround in the connection factory is to use connect.__wrapped__ instead of connect - then the connections are single wrapped.

@mjgp2 mjgp2 changed the title Using pscopg2 connection_factory throws exceptions Using psycopg2 connection_factory throws exceptions Sep 6, 2023
@wangzlei
Copy link
Contributor

wangzlei commented Sep 15, 2023

Thanks for reporting the issue. Could you provide

  1. the full source code of reproducing and the workaround by using connect.__wrapped__,
  2. the exception stack logs

These can be great helpful to us in fixing the issue faster.

@mjgp2
Copy link
Author

mjgp2 commented Nov 15, 2023

I can't get you stack logs, but here's a more complete example which basically does nothing, but if you try calling connect directly in the connection factory, you will see the error:

from psycopg2 import connect
def connection_factory(uri: str) -> Callable[[], connection]:
    do_connect = getattr(connect, "__wrapped__", connect)
    def connection_factory() -> connection:
        return do_connect(uri)
    return connection_factory

@jj22ee
Copy link
Contributor

jj22ee commented Apr 8, 2024

Hi @mjgp2, I am not able to reproduce locally. Could you help provide full code example of how you setup psycopg2 along with the aws-xray-sdk-python instrumentation for psycopg2? Is it still not possible to provide additional error logs?

@mjgp2
Copy link
Author

mjgp2 commented Apr 23, 2024

I think perhaps it's similar to this issue: #350

I really should have given better steps to reproduce at the time, apologies.

I'm going to close this for now.

@mjgp2 mjgp2 closed this as completed Apr 23, 2024
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

No branches or pull requests

3 participants