[Rd] Bug 17432 in readLines with R >= 3.5.0 still a problem

Chris Culnane chri@topher@culn@ne @ending from unimelb@edu@@u
Wed Sep 12 09:31:30 CEST 2018


Bug 17432 (https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17432) is still a problem when using pipes for IPC. 

The bug is evident when calling R from another process and trying to communicate via StdIn. R will buffer the input and not read lines until the buffer is exceeded or StdIn is closed by the sending process. This prevents interactive communication between a calling process and a child R process. 

>From a quick look at the source code, it looks like the bug is caused by only disabling buffering when isatty() returns true for a file descriptor (connections.c). This fixes the original bug when the script is run in a terminal, but doesn't help for pipes, which will return false for isatty().

An example R script and python script are provided to demonstrate the problem:

R script (example.r):
================
f <- file("stdin")
open(f)
while(length(line <- readLines(f,n=1)) > 0) {
  write(line, stderr())
}

Python3 script:
============
import sys, os, subprocess
process = subprocess.Popen(['Rscript', 'example.r'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in sys.stdin:
    process.stdin.write((line + '\n').encode('utf-8'))
    process.stdin.flush()


Expected Behaviour:
Run python script, each line entered is echoed back immediately by the R script - which is what happens on 3.4.4

Observed Behaviiour on >=3.5.0 (include devel):
The R script does not process lines as they are sent, it only receives them when StdIn is closed.


Best Regards

Chris 



More information about the R-devel mailing list