[Python-ideas] context managers used to allocate resources can be abused - a solution

Nick Coghlan ncoghlan at gmail.com
Sat Dec 17 03:07:55 CET 2011


On Sat, Dec 17, 2011 at 11:00 AM, Alon Horev <alon at horev.net> wrote:
> Hello,
>
> while I was implementing a connection pool i've noticed a pitfall of our
> beloved with statements:
>
> with pool.get_connection() as conn:
> ....conn.execute(...)
> conn.execute(...) # the connection has been returned to the pool and does
> not belong to the user!

That's a bug in the connection pool implementation. If the underlying
connection has been returned to the pool, the proxy returned by the
context manager shouldn't work any more.

Try the above code structure with a file object and it will give you
"IO operation on closed file".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list