Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

unix: fix reopened fd bug #971

Closed
wants to merge 6 commits into from
Closed

unix: fix reopened fd bug #971

wants to merge 6 commits into from

Conversation

indutny
Copy link
Contributor

@indutny indutny commented Oct 25, 2013

When fd is closed and new one (with the same number) is opened inside
kqueue/epoll/port loop's callback - stale events might invoke callbacks
on wrong watchers.

Check if watcher was changed after invocation and invalidate all events
with the same fd.

fix #826

/cc @bnoordhuis

@bnoordhuis
Copy link
Contributor

Here's a comment for you: where's the test case? :-)

@indutny
Copy link
Contributor Author

indutny commented Oct 30, 2013

Haha, actually this one would be quite complicated. But I'll do my best.

When fd is closed and new one (with the same number) is opened inside
kqueue/epoll/port loop's callback - stale events might invoke callbacks
on wrong watchers.

Check if watcher was changed after invocation and invalidate all events
with the same fd.

fix #826
@indutny
Copy link
Contributor Author

indutny commented Nov 2, 2013

@bnoordhuis added test.

unsigned int nwatchers;
unsigned int i;

if (len <= loop->nwatchers)
return;

nwatchers = next_power_of_two(len);
watchers = realloc(loop->watchers, nwatchers * sizeof(loop->watchers[0]));
nwatchers = next_power_of_two(len + 2) - 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic is unsound. Say that len == 14. In that case, next_power_of_two(14 + 2) - 2 is still 14.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, and that's ok. Since I'm allocating nwatchers + 2 below.

@indutny
Copy link
Contributor Author

indutny commented Nov 3, 2013

@bnoordhuis fixed all mentioned nits, except conversion to (int). Waiting for your reply.

@indutny
Copy link
Contributor Author

indutny commented Nov 3, 2013

And thank you :)

@indutny
Copy link
Contributor Author

indutny commented Nov 3, 2013

Fixed all nits, again :)

@indutny
Copy link
Contributor Author

indutny commented Nov 10, 2013

@bnoordhuis time to review it? :)

/* Copy watchers, preserving fake one in the end */
if (loop->watchers == NULL) {
fake_watcher_list = watchers[loop->nwatchers];
fake_watcher_count = watchers[loop->nwatchers + 1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Using loop->watchers here would be a little clearer.

ASSERT(status == 0);
ASSERT(connect_reqs <= req &&
req <= (connect_reqs + ARRAY_SIZE(connect_reqs)));
i = (unsigned int) (req - connect_reqs) / sizeof(*req);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just req - connect_reqs?

@bnoordhuis
Copy link
Contributor

Few comments but mostly LGTM. A possible future optimization is to store the current index in loop->watchers so you only have to scan (nfds - i) entries instead of nfds entries.

@indutny
Copy link
Contributor Author

indutny commented Nov 12, 2013

Landed with fixes in bbccafb and f50ccd5. Merged into the master.

@indutny indutny closed this Nov 12, 2013
@indutny indutny deleted the fix/gh-826 branch November 12, 2013 11:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants