I'm creating a tcp server who accept all the connection and execute incomming data has command line, but when i send "exit" to the tcpsocket, the process and the socket dont close properly
# main.cr
require "socket"
PORT = 2022
def handle_connection(socket)
Process.run("/bin/sh", input: socket, output: socket, error: socket)
end
server = TCPServer.new(PORT)
loop do
if socket = server.accept?
spawn handle_connection(socket)
else
break
end
end
for example, the following code work fine, after sending "exit" to STDIN, the shell is exiting, "process ending" is printedand the program close
channel = Channel(Nil).new
spawn do
Process.run("/bin/sh", input: STDIN, output: STDOUT, error: STDERR)
puts "process ending"
channel.send(nil)
end
channel.receive
for debuggin purpose i have tested this code too but "process ending" was never print until i manually close the tcp socket
# main.cr
require "socket"
PORT = 2022
def handle_connection(socket)
Process.run("/bin/sh", input: socket, output: socket, error: socket)
puts "process ending"
end
server = TCPServer.new(PORT)
loop do
if socket = server.accept?
spawn handle_connection(socket)
else
break
end
end
when i run main.cr, nc localhost 2022
and send "exit" i expect the socket close properly but he dont, and when i send more command the program raise an error
Unhandled exception in spawn: Error writing file: Broken pipe (Errno)
from /usr/lib/crystal/crystal/system/unix/file_descriptor.cr:79:13 in 'unbuffered_write'
from /usr/lib/crystal/io/buffered.cr:122:14 in 'write'
from /usr/lib/crystal/io.cr:1130:7 in 'copy'
from /usr/lib/crystal/process.cr:413:7 in 'copy_io'
from /usr/lib/crystal/process.cr:409:11 in 'copy_io:close_dst'
from /usr/lib/crystal/process.cr:298:17 in '->'
from /usr/lib/crystal/fiber.cr:255:3 in 'run'
from /usr/lib/crystal/fiber.cr:47:34 in '->'
from ???