Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions python/pyspark/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ def handle_sigterm(*args):
if pid == 0:
# in child process
listen_sock.close()

# close stdin for child process, see SPARK-26175
# but we cannot just close it, because it make fileno "0" unallocated
# and cause later new created file descriptor use fileno "0"
# so here I close fd 0 and reopen it on "/dev/null"
dummy_input = open(os.devnull, 'r')
os.dup2(dummy_input.fileno(), 0)
dummy_input.close()
Comment thread
HyukjinKwon marked this conversation as resolved.
Outdated

try:
# Acknowledge that the fork was successful
outfile = sock.makefile(mode="wb")
Expand Down
14 changes: 14 additions & 0 deletions python/pyspark/sql/tests/test_udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,20 @@ def f():

self.spark.range(1).select(f()).collect()

def test_worker_original_stdin_closed(self):
# Test the original stdin of worker inherit from daemon is closed
Comment thread
HyukjinKwon marked this conversation as resolved.
Outdated
# and is replaced with '/dev/null'.
# See SPARK-26175
def task(iterator):
import sys
res = sys.stdin.read()
# Because the stdin is replaced with '/dev/null'
Comment thread
HyukjinKwon marked this conversation as resolved.
Outdated
# Read data from it will get EOF
assert res == '', "Expect read EOF from stdin."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Verify read stdin get EOF immediately.
Should we add more test such as verifying the worker process actually exit ?
But I think current test is enough, the fact we can only read EOF from stdin represent the stdin is dummy and safe file descriptor, it won't influence other file descriptors in daemon.

return iterator

self.sc.parallelize(range(1), 1).mapPartitions(task).count()


class UDFInitializationTests(unittest.TestCase):
def tearDown(self):
Expand Down