From jeff.a.ryan at gmail.com  Mon May 18 19:40:14 2009
From: jeff.a.ryan at gmail.com (Jeff Ryan)
Date: Mon, 18 May 2009 12:40:14 -0500
Subject: [Rd] readBin on binary non-blocking connections (Windows & Unix
	differences/bugs)
Message-ID: <e8e755250905181040k593e8b5al4725972d109c4d8a@mail.gmail.com>

R-devel:

I am encountering a consistency issue using socketConnection and
readBin with *non-blocking* connections on Unix and Windows XP (no
Vista to test).

I am a bit confused by the behavior of *non-blocking* connections
under Windows specifically.  When calling readBin on a non-blocking
connection when there is no data to read on the socket, the connection
under Unix will return a vector of zero-length matching the type of
the 'what' argument to the readBin call.

The same call under Windows returns random data unless called with
what=raw(), which instead returns an error.


A simple example using two R processes to illustrate on one machine:


#########################################################################
# PROCESS 1 (Windows/Unix -- the same code for both):
#########################################################################

# Open the connection and don't write anything

s <- socketConnection(port=7777, server=TRUE, blocking=TRUE,open="ab")

#########################################################################
# PROCESS 2 (Windows) use this to read:
#########################################################################

s <- socketConnection(port=7777,blocking=FALSE,open="ab")
readBin(s, character())
readBin(s, character())
readBin(s, double())
readBin(s, raw())

# > readBin(s, character())
# [1] "\020??\001@??\001 $?\001 $?\001 $?\001 $?\001 $?\001 $?\001
$?\001 $?\001 ...
# > readBin(s, character())
# [1] "X\n"
# > readBin(s, double())
# [1] 1.255609e-316
# > readBin(s, raw())
# Error in readBin(s, raw()) : negative length vectors are not allowed

##########################################################################
# Using a *nix, the above works as I would expect
#
# PROCESS 2 (Unixes -- this example OSX 10.4, but is consistent on
other flavors)
#########################################################################

s <- socketConnection(port=7777,blocking=FALSE, open="ab")
readBin(s, character())
readBin(s, raw())
readBin(s, double())

# > readBin(s, character())
# character(0)
# > readBin(s, raw())
# raw(0)
# > readBin(s, double())
# numeric(0)

Is this a bug in R or Windows?  Is there a workaround?  I would like
to have readBin on non-blocking connections work consistently across
platforms.  When data is present, both platforms behave consistently.
Incidentally, the readBin calls in my program are not using R as the
server, the above code is simply to illustrate the problem.

The fact that the raw() returns an error on Windows seems to indicate
that the error is able to be caught, and could be tested for.

One other interesting point of note; the character() calls on Windows
will return not just random data, but very non-random R type strings.
Somehow non-allocated memory is being read.

> readBin(s, character(),100)
  [1] "X\n"
  [2] "spatch"
  [3] ""
  [4] ""
  [5] "X\n"
  [6] "ssion"
  [7] "\002"
  [8] "?$?\001?|?\001\030[?\001?N?\001?N?\001?$?\001\004"
  [9] "H\024?\001\f\025?\001,??\001\002\002"
 [10] "?\026?\001?$?\001\005"
 [11] ""
 [12] "s.logical"
...
 [75] "8}?\001?~?\001\020??\001?$?\001?}?\001?$?\001???\001
$?\001???\001?~?\0014~?\001???\001?|?\001???\001???\001\034}?\001???\001
$?\001?|?\001?\177?\001"
 [76] "?$?\001?$?\001???\001\030??\001T??\001?$?\001?$?\001
$?\001???\0010??\001?$?\001?$?\001???\0014??\001
\177?\001X??\001\034??\001?$?\001???\001???\001
|?\001\004??\001???\0010\177?\001???\001?{?\001<??\001
$?\001???\001?}?\001 ??\001?$?\001?$?\001\024\177?\001
$?\001t??\001???\001?}?\001?$?\001?$?\001t|?\001?$?\001
$?\001?\177?\001???\001?|?\001 |?\001?$?\001d??\001
$?\001???\001x{?\001?$?\001"
 [77] "' must be of length 1"

This behavior has been noticed by me at least as early as 2.6, and is
currently seen under 2.9.0.

Thanks for any pointers,
Jeff Ryan


-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



[Rd] readBin on binary non-blocking connections (Windows & Unix differences/bugs)
Jeff Ryan