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

Using Copy.rows breaks Connection context manager #23

Closed
benesch opened this issue Jan 12, 2021 · 1 comment
Closed

Using Copy.rows breaks Connection context manager #23

benesch opened this issue Jan 12, 2021 · 1 comment

Comments

@benesch
Copy link

benesch commented Jan 12, 2021

Using the awesome new Copy.rows method works great while the COPY operation is running, but appears to leave the connection in an unclean state. Attempting to run another query on the connection, or exit the context manager for the connection, results in "sending query failed: another command is already in progress".

$ cat repro.py
from psycopg3.oids import builtins
import psycopg3

with psycopg3.connect() as conn:
    with conn.cursor() as cur:
        with cur.copy("COPY (VALUES (1), (2), (3)) TO STDOUT (FORMAT BINARY)") as copy:
            copy.set_types([builtins["int4"].oid])
            for row in copy.rows():
                print(row)

$ python repro.py 
(1,)
(2,)
(3,)
Traceback (most recent call last):
  File "repro.py", line 9, in <module>
    print(row)
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/connection.py", line 435, in __exit__
    self.commit()
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/connection.py", line 465, in commit
    self.wait(self._commit_gen())
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/connection.py", line 510, in wait
    return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/waiting.py", line 223, in wait_epoll
    s = next(gen)
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/connection.py", line 380, in _commit_gen
    yield from self._exec_command(b"commit")
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/connection.py", line 346, in _exec_command
    self.pgconn.send_query(command)
  File "/home/benesch/Sites/psycopg3-play/venv/lib/python3.8/site-packages/psycopg3/pq/pq_ctypes.py", line 242, in send_query
    raise PQerror(f"sending query failed: {error_message(self)}")
psycopg3.pq.PQerror: sending query failed: another command is already in progress
@benesch
Copy link
Author

benesch commented Jan 12, 2021

Thanks very much! The fix indeed works for us.

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

1 participant