bpo-42064: Pass module state to `sqlite3` UDF callbacks by erlend-aasland · Pull Request #27456 · python/cpython

@erlend-aasland

  • Establish common callback context struct
  • Convert UDF callbacks to fetch module state from callback context

https://bugs.python.org/issue42064

Automerge-Triggered-By: GH:encukou

Erlend E. Aasland added 2 commits

July 29, 2021 21:01

@erlend-aasland

A couple of questions, @encukou:

  • Do I need to ensure GIL is held in create_callback_context(), or can I get away with it like it is?
  • Ditto for free_callback_context()

@encukou

Do I need to ensure GIL is held in create_callback_context(), or can I get away with it like it is?

There are very few funcitons in the C-API that you can call without the GIL held; usually they're the ones needed before a GIL is created. For PyMem_Malloc/PyMem_Free specifically, there's a a big red warning to answer your question ;)
PyMem_Malloc is configurable; the default allocator relies on the GIL for synchronization.

@erlend-aasland

There are very few funcitons in the C-API that you can call without the GIL held; usually they're the ones needed before a GIL is created. For PyMem_Malloc/PyMem_Free specifically, there's a a big red warning to answer your question ;)

Yes, I've seen it ;) But then why does this PR work as it is? (That is, without the GIL)

(FYI: I'll update the PR with GIL wrappers soon)

@encukou

But then why does this PR work as it is?

I assume it's because you're not using multiple threads.
Even if you were, it might be rare to cause the memory allocator to run in two threads at once. But when it would, it'd crash quite spectacularly. (Or worse, if a malicious actor could cause it to happen.)

@erlend-aasland

But then why does this PR work as it is?

I assume it's because you're not using multiple threads.
Even if you were, it might be rare to cause the memory allocator to run in two threads at once. But when it would, it'd crash quite spectacularly. (Or worse, if a malicious actor could cause it to happen.)

Ah, right. Thanks!

I've updated the PR. PTAL :)

@erlend-aasland

On hold until #27613 has landed.

@encukou, this is now ready for review (again). PTAL.

encukou

Erlend E. Aasland added 2 commits

August 24, 2021 13:54

encukou

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some more questionable uses of the GIL in existing code (see the issue). But let's not block this PR; it is an improvement.

@erlend-aasland