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

conn.copy_to_table() empties source file and throws exception #574

Closed
sergeyspatar opened this issue May 14, 2020 · 1 comment
Closed

conn.copy_to_table() empties source file and throws exception #574

sergeyspatar opened this issue May 14, 2020 · 1 comment

Comments

@sergeyspatar
Copy link

  • asyncpg version: 0.20.1
  • PostgreSQL version: 12.2
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : I use local PostgreSQL
  • Python version: 3.8.3rc1
  • Platform: Linux billing 5.4.0-4-cloud-amd64 Other examples? #1 SMP Debian 5.4.19-1 (2020-02-13) x86_64 GNU/Linux
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and uvloop?: Yes

Steps to reproduce:

  1. Create CSV file "copy_test.csv":
echo "Hello,World" > copy_test.csv
  1. This script creates table "copy_test" with two columns and tries to import file by passing file path to conn.copy_to_table():
import asyncio
import asyncpg


async def main():
    conn = await asyncpg.connect(...)
    await conn.execute("create table copy_test (c1 varchar, c2 varchar)")
    await conn.copy_to_table(
        'copy_test',
        source='copy_test.csv',
        format='csv'
    )


#import uvloop
#asyncio.set_event_loop(uvloop.new_event_loop())

asyncio.run(main())

Script throws exception:

$ python3 test_asyncpg_copy_to_table.py
Traceback (most recent call last):
  File "test_asyncpg_copy_to_table.py", line 18, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "test_asyncpg_copy_to_table.py", line 8, in main
    await conn.copy_to_table(
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 666, in copy_to_table
    return await self._copy_in(copy_stmt, source, timeout)
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 845, in _copy_in
    return await self._protocol.copy_in(
  File "asyncpg/protocol/protocol.pyx", line 482, in copy_in
  File "asyncpg/protocol/protocol.pyx", line 455, in asyncpg.protocol.protocol.BaseProtocol.copy_in
  File "asyncpg/protocol/protocol.pyx", line 456, in asyncpg.protocol.protocol.BaseProtocol.copy_in
  File "/usr/lib/python3.8/asyncio/tasks.py", line 455, in wait_for
    return await fut
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 836, in __anext__
    data = await run_in_executor(None, f.read, 524288)
  File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
io.UnsupportedOperation: read

And the file "copy_test.csv" is now 0 bytes.

@sergeyspatar sergeyspatar changed the title conn.copy_to_table() erases source file and throws exception conn.copy_to_table() empties source file and throws exception May 14, 2020
@elprans
Copy link
Member

elprans commented May 14, 2020

This was fixed in #555.

@elprans elprans closed this as completed May 14, 2020
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

2 participants