digital-fabric / polyphony

Fine-grained concurrency for Ruby
https://www.rubydoc.info/gems/polyphony
MIT License
655 stars 17 forks source link

TCPSocket#read returns inconsistent results #102

Closed floriandejonckheere closed 1 year ago

floriandejonckheere commented 1 year ago

When calling TCPSocket#read with a number, Polyphony seems to read an inconsistent number of bytes, possibly related to whether the data is contained in a single TCP packet or in multiple. Given the scripts below, running them a number of times result in different output. Plain Ruby reads the entire data correctly.

server.rb ```ruby require "socket" # Length of data to send LENGTH = 8192 server = TCPServer.new("0.0.0.0", 9000) while (socket = server.accept) # Send header socket << LENGTH.to_s # Send data ("x" * LENGTH).chars.each_slice(1024).map(&:join).each do |slice| puts "Sending #{slice.length} bytes" socket << slice end socket.close end server.stop ```
client.rb ```ruby if ARGV.first == "--polyphony" require "polyphony" else require "socket" end socket = TCPSocket.new("127.0.0.1", 9000) # Read header (4 bytes) length = socket.read(4).to_i # Read data puts "Reading #{length} bytes" data = socket.read(length) puts "Received #{data.length} bytes" socket.close ```

Output:

/srv # bin/client
Reading 8192 bytes
Received 8192 bytes

/srv # bin/client --polyphony
Reading 8192 bytes
Received 2048 bytes
/srv # bin/client --polyphony
Reading 8192 bytes
Received 1024 bytes
/srv # bin/client --polyphony
Reading 8192 bytes
Received 2048 bytes
/srv # bin/client --polyphony
Reading 8192 bytes
Received 4096 bytes