Skip to content

Commit a3acf40

Browse files
authored
Implements delayed pipe_notify(flag = tools::SIGTERM) (#213)
* Delayed SIGTERM concept * Add docs and news item * Advance dev version
1 parent e9abd16 commit a3acf40

File tree

8 files changed

+32
-12
lines changed

8 files changed

+32
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: nanonext
33
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
4-
Version: 1.7.1.9000
4+
Version: 1.7.1.9001
55
Authors@R: c(
66
person("Charlie", "Gao", , "[email protected]", role = c("aut", "cre"),
77
comment = c(ORCID = "0000-0002-0750-061X")),

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Updates
44

5+
* `pipe_notify(flag = tools::SIGTERM)` will now raise the signal with a 100ms grace period to allow a process to exit normally (#212).
56
* `parse_url()` drops 'rawurl', 'host' and 'requri' from the output vector as these can be derived from the other parts.
67

78
# nanonext 1.7.1

R/aio.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ stop_request <- function(x) invisible(.Call(rnng_request_stop, x))
303303
#' while (unresolved(aio)) {
304304
#' # do stuff before checking resolution again
305305
#' cat("unresolved\n")
306-
#' msleep(20)
306+
#' msleep(100)
307307
#' }
308308
#'
309309
#' unresolved(aio)

R/sync.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ cv_signal <- function(cv) invisible(.Call(rnng_cv_signal, cv))
167167
#' signal, and causes any subsequent [wait()] to return FALSE instead of TRUE.
168168
#' If a signal from the \pkg{tools} package, e.g. `tools::SIGINT`, or an
169169
#' equivalent integer value is supplied, this sets a flag and additionally
170-
#' raises this signal upon the flag being set.
170+
#' raises this signal upon the flag being set. For `tools::SIGTERM`, the
171+
#' signal is raised with a 100ms grace period to allow a process to exit
172+
#' normally.
171173
#'
172174
#' @return Invisibly, zero on success (will otherwise error).
173175
#'

man/pipe_notify.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/unresolved.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nanonext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extern int R_interrupts_pending;
124124
#define NANONEXT_CHUNK_SIZE 67108864 // must be <= INT_MAX
125125
#define NANONEXT_STR_SIZE 40
126126
#define NANONEXT_WAIT_DUR 1000
127+
#define NANONEXT_SLEEP_DUR 100
127128
#define NANO_ALLOC(x, sz) \
128129
(x)->buf = calloc(sz, sizeof(unsigned char)); \
129130
if ((x)->buf == NULL) Rf_error("memory allocation failed"); \

src/sync.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ static void request_complete(void *arg) {
120120

121121
}
122122

123+
void delayed_sigterm(void *arg) {
124+
(void) arg;
125+
nng_msleep(NANONEXT_SLEEP_DUR);
126+
#ifdef _WIN32
127+
raise(SIGTERM);
128+
#else
129+
kill(getpid(), SIGTERM);
130+
#endif
131+
}
132+
123133
void pipe_cb_signal(nng_pipe p, nng_pipe_ev ev, void *arg) {
124134

125135
int sig;
@@ -134,16 +144,20 @@ void pipe_cb_signal(nng_pipe p, nng_pipe_ev ev, void *arg) {
134144
nng_cv_wake(cv);
135145
nng_mtx_unlock(mtx);
136146
if (sig > 1) {
147+
if (sig == SIGTERM) {
148+
nng_thread *thr;
149+
nng_thread_create(&thr, delayed_sigterm, NULL);
150+
} else {
137151
#ifdef _WIN32
138-
if (sig == SIGINT)
139-
UserBreak = 1;
140-
raise(sig);
152+
if (sig == SIGINT)
153+
UserBreak = 1;
154+
raise(sig);
141155
#else
142-
if (sig == SIGINT)
143-
R_interrupts_pending = 1;
144-
kill(getpid(), sig);
156+
if (sig == SIGINT)
157+
R_interrupts_pending = 1;
158+
kill(getpid(), sig);
145159
#endif
146-
160+
}
147161
}
148162

149163
}

0 commit comments

Comments
 (0)