The problem with contexts is that can't just innocently enter one in some if branch where the rest of the code would be scoped.
I ended up an OnExit() context that does:
if conn is not None:
ctx.call(conn.close)