Skip to content

Commit c08ccd5

Browse files
committed
New approach
1 parent 40105b6 commit c08ccd5

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

R/pkg/inst/worker/daemon.R

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ inputCon <- socketConnection(
3333
# Waits indefinitely for a socket connecion by default.
3434
selectTimeout <- NULL
3535

36-
# Exit code that children send to the parent to indicate they exited.
37-
exitCode <- 1
38-
3936
while (TRUE) {
4037
ready <- socketSelect(list(inputCon), timeout = selectTimeout)
4138

@@ -56,19 +53,17 @@ while (TRUE) {
5653
# any worker is running or right before launching other worker children from the following
5754
# new socket connection.
5855

59-
# Only the process IDs of children sent data to the parent are returned below. The children
60-
# send a custom exit code to the parent after being exited and the parent tries
61-
# to terminate them only if they sent the exit code.
56+
# The process IDs of exited children are returned below.
6257
children <- parallel:::selectChildren(timeout = 0)
6358

6459
if (is.integer(children)) {
6560
lapply(children, function(child) {
66-
# This data should be raw bytes if any data was sent from this child.
67-
# Otherwise, this returns the PID.
68-
data <- parallel:::readChild(child)
69-
if (is.raw(data)) {
70-
# This checks if the data from this child is the exit code that indicates an exited child.
71-
if (unserialize(data) == exitCode) {
61+
# This should be the PIDs of exited children. Otherwise, this returns raw bytes if any data
62+
# was sent from this child. In this case, we discard it.
63+
pid <- parallel:::readChild(child)
64+
if (is.integer(pid)) {
65+
# This checks if the data from this child is the same pid of this selected child.
66+
if (child == pid) {
7267
# If so, we terminate this child.
7368
tools::pskill(child, tools::SIGUSR1)
7469
}
@@ -95,9 +90,8 @@ while (TRUE) {
9590
close(inputCon)
9691
Sys.setenv(SPARKR_WORKER_PORT = port)
9792
try(source(script))
98-
# Note that this mcexit does not fully terminate this child. So, this writes back
99-
# a custom exit code so that the parent can read and terminate this child.
100-
parallel:::mcexit(0L, send = exitCode)
93+
# Note that this mcexit does not fully terminate this child.
94+
parallel:::mcexit(0L)
10195
} else {
10296
# Forking succeeded and we need to check if they finished their jobs every second.
10397
selectTimeout <- 1

0 commit comments

Comments
 (0)