Skip to content

gh-151364: Add critical sections to kqueue#151376

Open
vstinner wants to merge 4 commits into
python:mainfrom
vstinner:kqueue_critical_section
Open

gh-151364: Add critical sections to kqueue#151376
vstinner wants to merge 4 commits into
python:mainfrom
vstinner:kqueue_critical_section

Conversation

@vstinner

@vstinner vstinner commented Jun 11, 2026

Copy link
Copy Markdown
Member

@vstinner

Copy link
Copy Markdown
Member Author

The bug report was only on .close() and closed, but I also added critical sections on __init__(), repr() and richcompare.

cc @kumaraditya303

Comment thread Modules/selectmodule.c Outdated
Comment on lines +1993 to +2001
static int
kqueue_event_init(PyObject *op, PyObject *args, PyObject *kwds)
{
int res;
Py_BEGIN_CRITICAL_SECTION(self);
res = kqueue_event_init_lock_held(op, args, kwds);
Py_END_CRITICAL_SECTION();
return res;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We very rarely add locking to __init__ functions because there are few reasons to support concurrent reinitialization on a live object, and it slows the single-threaded case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

_io.TextIOWrapper.__init__ was recently modified to add @critical_section (commit db4b194). Was it a bad idea? Or it's just that it should be an exception, and not the general rule?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated the PR to not use a critical section on kqueue_event_init().

@@ -0,0 +1,2 @@
Add critical sections to :func:`select.kqueue` objects. Patch by Victor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do users know what "critical sections" mean in this case? It's probably better to say something like "Fix thread-safety issues in select.kqueue objects," because that's what users care more about.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea, I updated the Changelog entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants