Skip to content

Commit 774eb3b

Browse files
authored
Merge pull request #2285 from oko256/zloop-fix-nonstop
zloop: ignore EINTR from poll if nonstop is enabled
2 parents db94044 + e88e211 commit 774eb3b

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/zloop.c

+48
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ zloop_start (zloop_t *self)
723723
}
724724
rc = zmq_poll (self->pollset, (int) self->poll_size, s_tickless (self));
725725
if (rc == -1 || (zsys_interrupted && !self->nonstop)) {
726+
if (errno == EINTR && self->nonstop) {
727+
rc = 0;
728+
continue;
729+
}
726730
if (self->verbose) {
727731
if (rc == -1)
728732
zsys_debug ("zloop: interrupted: %s", strerror (errno));
@@ -915,6 +919,22 @@ s_timer_event5 (zloop_t *loop, int timer_id, void *arg)
915919
return 0;
916920
}
917921

922+
static void
923+
s_raise_sigint_actor (zsock_t *pipe, void *args)
924+
{
925+
zsock_signal (pipe, 0);
926+
assert (zsys_interrupted == 0);
927+
zsock_wait (pipe);
928+
zclock_sleep (100);
929+
#if defined (__WINDOWS__)
930+
assert (GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0) != 0);
931+
#else
932+
assert (kill (getpid(), SIGINT) == 0);
933+
#endif
934+
zclock_sleep (100);
935+
assert (zsys_interrupted != 0);
936+
}
937+
918938
void
919939
zloop_test (bool verbose)
920940
{
@@ -976,6 +996,34 @@ zloop_test (bool verbose)
976996
assert (timer_event_called);
977997
zsys_interrupted = 0;
978998

999+
// Check that SIGINT terminates loop if nonstop is not set
1000+
zloop_destroy (&loop);
1001+
zactor_t *raise_sigint_actor = zactor_new (s_raise_sigint_actor, NULL);
1002+
assert (raise_sigint_actor);
1003+
loop = zloop_new ();
1004+
zloop_set_nonstop (loop, false);
1005+
timer_event_called = false;
1006+
zloop_timer (loop, 1000, 1, s_timer_event3, &timer_event_called);
1007+
zsock_signal (raise_sigint_actor, 0);
1008+
zloop_start (loop);
1009+
zactor_destroy (&raise_sigint_actor);
1010+
assert (!timer_event_called);
1011+
zsys_interrupted = 0;
1012+
1013+
// Check that SIGINT does not terminate the loop if nonstop is set
1014+
zloop_destroy (&loop);
1015+
raise_sigint_actor = zactor_new (s_raise_sigint_actor, NULL);
1016+
assert (raise_sigint_actor);
1017+
loop = zloop_new ();
1018+
zloop_set_nonstop (loop, true);
1019+
timer_event_called = false;
1020+
zloop_timer (loop, 500, 1, s_timer_event3, &timer_event_called);
1021+
zsock_signal (raise_sigint_actor, 0);
1022+
zloop_start (loop);
1023+
zactor_destroy (&raise_sigint_actor);
1024+
assert (timer_event_called);
1025+
zsys_interrupted = 0;
1026+
9791027
// Check if reader removed in timer is not called
9801028
zloop_destroy (&loop);
9811029
loop = zloop_new ();

src/zsock.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ zsock_vsend (void *self, const char *picture, va_list argptr)
942942
zframe_t *frame = zlistx_pack (list);
943943
zmsg_append (msg, &frame);
944944
}
945-
#endif
945+
#endif
946946
else
947947
if (*picture == 'm') {
948948
zframe_t *frame;
@@ -1171,7 +1171,7 @@ zsock_vrecv (void *self, const char *picture, va_list argptr)
11711171
}
11721172
zframe_destroy (&frame);
11731173
}
1174-
#ifdef CZMQ_BUILD_DRAFT_API
1174+
#ifdef CZMQ_BUILD_DRAFT_API
11751175
else
11761176
if (*picture == 'l') {
11771177
zframe_t *frame = zmsg_pop (msg);
@@ -2205,7 +2205,7 @@ zsock_test (bool verbose)
22052205
#ifdef ZMQ_STREAM
22062206
zsock_t *streamrecv = zsock_new(ZMQ_STREAM);
22072207
assert (streamrecv);
2208-
port = zsock_bind(streamrecv, "tcp://*:*");
2208+
port = zsock_bind(streamrecv, "tcp://127.0.0.1:*");
22092209
assert(port > 0);
22102210

22112211
zsock_t *streamsender = zsock_new(ZMQ_STREAM);
@@ -2398,7 +2398,7 @@ zsock_test (bool verbose)
23982398
char* message;
23992399
message = zstr_recv (gather);
24002400
assert (streq(message, "HELLO"));
2401-
zstr_free (&message);
2401+
zstr_free (&message);
24022402

24032403
zsock_destroy (&gather);
24042404
zsock_destroy (&scatter);

0 commit comments

Comments
 (0)