socketry/async-io

v1.23.0 breaks README example

tonytonyjan opened this issue · 2 comments

The example:

require 'async/io'

def echo_server(endpoint)
	Async do |task|
		# This is a synchronous block within the current task:
		endpoint.accept do |client|
			# This is an asynchronous block within the current reactor:
			data = client.read(512)
			
			# This produces out-of-order responses.
			task.sleep(rand * 0.01)
			
			client.write(data.reverse)
		end
	end
end

def echo_client(endpoint, data)
	Async do |task|
		endpoint.connect do |peer|
			result = peer.write(data)
			
			message = peer.read(512)
			
			puts "Sent #{data}, got response: #{message}"
		end
	end
end

Async do
	endpoint = Async::IO::Endpoint.tcp('0.0.0.0', 9000)
	
	server = echo_server(endpoint)
	
	5.times.collect do |i|
		echo_client(endpoint, "Hello World #{i}")
	end.each(&:wait)
	
	server.stop
end

The output with rsync-io v1.22.0:

Sent Hello World 2, got response: 2 dlroW olleH
Sent Hello World 0, got response: 0 dlroW olleH
Sent Hello World 1, got response: 1 dlroW olleH
Sent Hello World 3, got response: 3 dlroW olleH
Sent Hello World 4, got response: 4 dlroW olleH

With v1.23.0, this example hangs, waiting for incoming data from client, while client has nothing to send.

It could have something to do whit this commit:

38cbf08#diff-0bd25964d6d64dbffce9d542ba85a492R110

Since all the data has been read during fast path, the secondsysread would never receive new data and block forever.

Hmm, that makes sense, I will check.

This change was made because of #23

It's the correct behaviour. I've updated the example.

Let me know if you have any more questions :)