This project is archived and is in readonly mode.
Possible multithreading problem in psycopg2.
-
Daniele Varrazzo
- State changed from new to open
I think you are right, thank you for the report.
At a first glance that seems the only place where we call psycopg_escape_string without GIL, but couldn't tell for other PyMem_Malloc.
The easiest fix is to drop the gil release around that call. The other option would be to use malloc instead, but psycopg_escape_string is used in several place and we should care to pair each use with the correct free(), which is a much more invasive change. Because the operation doesn't perform any I/O, I don't think releasing the GIL there is really worth, so I'd keep it simple.
Maybe the fact we have never hit this bug before is due to the time spent into that gil release being very negligible.
Fog, do you agree with this solution?
-
Manu Cupcic
At a first glance that seems the only place where we call psycopg_escape_string without GIL, but couldn't tell for other PyMem_Malloc.
I came to the same conclusion (I am no expert though).
The easiest fix is to drop the gil release around that call. The other option would be to use malloc instead, but psycopg_escape_string is used in several place and we should care to pair each use with the correct free(), which is a much more invasive change. Because the operation doesn't perform any I/O, I don't think releasing the GIL there is really worth, so I'd keep it simple.
Agree with that too, not sure how long this call takes relative to an actual call to the database. Although the PQescapeStringConn function needs a pointer to a connection, so maybe it does something longish ?
Maybe the fact we have never hit this bug before is due to the time spent into that gil release being very negligible.
Yes. Possibly
Thanks for the really quick response
Manu
-
Federico Di Gregorio
I'd avoid switching to malloc() because, at least in theory, PyMem_Malloc tells the interpreter how much memory pressure we're putting on it and that's a Good Thing. So, yes, removing the GIL release is the best thing, IMHO.
-
Manu Cupcic
Thanks,
I would just like to know if this is going to mean a new release soon, or if I should build the module from a patched fork for the time being.
Manu
-
Daniele Varrazzo
This bug was terrible, I just wonder why it hasn't bit us before. So I'm for releasing soon, even if its longevity tells it's not hot urgent.
I try and run the full tests tonight or tomorrow at most. I don't think we have anything outstanding stopping us to release.