Possible data race in psycopg2

From: Mark Charsley <mcharsley(at)google(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: Possible data race in psycopg2
Date: 2020-10-14 14:20:05
Message-ID: CAAf4L0eqm6hozAZyKP005a6UEPnnQcQe-=qznbnOr6B-tQSCAw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Hi. I've started a little project that uses psycopg2, and hit a problem
when running tests under ThreadSanitizer:

WARNING: ThreadSanitizer: data race (pid=9435)
Read of size 4 at 0x7b44000aae38 by thread T7:
#0 psyco_conn_cursor psycopg2/psycopg/connection_type.c:88:15 (_
psycopg.abi3.so+0x2a434)

Previous write of size 4 at 0x7b44000aae38 by thread T6 (mutexes: write
M142280395227573736):
#0 pq_begin_locked psycopg2/psycopg/pqpath.c:377:22 (_psycopg.abi3.so
+0x38766)
#1 _pq_execute_sync psycopg2/psycopg/pqpath.c:819:22 (_psycopg.abi3.so
+0x3923e)
#2 pq_execute psycopg2/psycopg/pqpath.c:947:16 (_psycopg.abi3.so
+0x3abd7)
#3 _psyco_curs_execute psycopg2/psycopg/cursor_type.c:447:11 (_
psycopg.abi3.so+0x32820)
#4 curs_execute psycopg2/psycopg/cursor_type.c:491:13 (_psycopg.abi3.so
+0x2f92b)

It's possible that this is caused by an underlying failure in the postgres
server, where thread sanitizer panics about a
safe-except-under-the-most-insanely-optimising-compiler issue, discussed
here:
https://www.postgresql.org/message-id/CAAf4L0d5XpgmX%2BnzT2G_F7EPkJb2JODr_QCzOsSvWBnXxLKYVQ%40mail.gmail.com
which may have killed the server (the tests I'm running compile everything,
including the DB server with tsan), and handling the dead connection causes
the issues mentioned above. But can wiser and more experienced heads than
mine cast their eyes over the code above, and see if there is indeed a
problem with a write to conn_status in pq_begin_lockedand a read from it in
psyco_conn_cursor without a memory barrier in-between?

thanks

Mark

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2020-11-07 16:32:39 psycopg3 and adaptation choices
Previous Message Adrian Klaver 2020-09-25 23:46:39 Re: iterating over DictRow