Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
214e25ff
Commit
214e25ff
authored
Nov 28, 2017
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix wrong poll/epoll timeout after EINTR.
parent
6cf025b4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
6 deletions
+56
-6
async-unix-test.c++
c++/src/kj/async-unix-test.c++
+37
-0
async-unix.c++
c++/src/kj/async-unix.c++
+19
-6
No files found.
c++/src/kj/async-unix-test.c++
View file @
214e25ff
...
...
@@ -35,6 +35,8 @@
#include <pthread.h>
#include <algorithm>
#include <sys/wait.h>
#include <sys/time.h>
#include <errno.h>
namespace
kj
{
namespace
{
...
...
@@ -576,6 +578,41 @@ TEST(AsyncUnixTest, SteadyTimers) {
}
}
bool
dummySignalHandlerCalled
=
false
;
void
dummySignalHandler
(
int
)
{
dummySignalHandlerCalled
=
true
;
}
TEST
(
AsyncUnixTest
,
InterruptedTimer
)
{
captureSignals
();
UnixEventPort
port
;
EventLoop
loop
(
port
);
WaitScope
waitScope
(
loop
);
// Schedule a timer event in 10ms.
auto
&
timer
=
port
.
getTimer
();
auto
start
=
timer
.
now
();
constexpr
auto
timeout
=
10
*
MILLISECONDS
;
// Arrange SIGALRM to be delivered in 5ms, handled in an empty signal handler. This will cause
// our wait to be interrupted with EINTR. We should nevertheless continue waiting for the right
// amount of time.
dummySignalHandlerCalled
=
false
;
if
(
signal
(
SIGALRM
,
&
dummySignalHandler
)
==
SIG_ERR
)
{
KJ_FAIL_SYSCALL
(
"signal(SIGALRM)"
,
errno
);
}
struct
itimerval
itv
;
memset
(
&
itv
,
0
,
sizeof
(
itv
));
itv
.
it_value
.
tv_usec
=
5000
;
// signal after 5ms
setitimer
(
ITIMER_REAL
,
&
itv
,
nullptr
);
timer
.
afterDelay
(
timeout
).
wait
(
waitScope
);
KJ_EXPECT
(
dummySignalHandlerCalled
);
KJ_EXPECT
(
timer
.
now
()
-
start
>=
timeout
);
KJ_EXPECT
(
timer
.
now
()
-
start
<=
timeout
+
(
timeout
/
5
));
// allow 2ms error
}
TEST
(
AsyncUnixTest
,
Wake
)
{
captureSignals
();
UnixEventPort
port
;
...
...
c++/src/kj/async-unix.c++
View file @
214e25ff
...
...
@@ -542,8 +542,18 @@ bool UnixEventPort::doEpollWait(int timeout) {
}
struct
epoll_event
events
[
16
];
int
n
;
KJ_SYSCALL
(
n
=
epoll_wait
(
epollFd
,
events
,
kj
::
size
(
events
),
timeout
));
int
n
=
epoll_wait
(
epollFd
,
events
,
kj
::
size
(
events
),
timeout
);
if
(
n
<
0
)
{
int
error
=
errno
;
if
(
error
==
EINTR
)
{
// We can't simply restart the epoll call because we need to recompute the timeout. Instead,
// we pretend epoll_wait() returned zero events. This will cause the event loop to spin once,
// decide it has nothing to do, recompute timeouts, then return to waiting.
n
=
0
;
}
else
{
KJ_FAIL_SYSCALL
(
"epoll_wait()"
,
error
);
}
}
bool
woken
=
false
;
...
...
@@ -724,13 +734,16 @@ public:
}
void
run
(
int
timeout
)
{
do
{
pollResult
=
::
poll
(
pollfds
.
begin
(),
pollfds
.
size
(),
timeout
);
pollError
=
pollResult
<
0
?
errno
:
0
;
// EINTR should only happen if we received a signal *other than* the ones registered via
// the UnixEventPort, so we don't care about that case.
}
while
(
pollError
==
EINTR
);
if
(
pollError
==
EINTR
)
{
// We can't simply restart the poll call because we need to recompute the timeout. Instead,
// we pretend poll() returned zero events. This will cause the event loop to spin once,
// decide it has nothing to do, recompute timeouts, then return to waiting.
pollResult
=
0
;
pollError
=
0
;
}
}
void
processResults
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment