{"action":"created","issue":{"url":"https://api.github.com/repos/crystal-lang/crystal/issues/6759","repository_url":"https://api.github.com/repos/crystal-lang/crystal","labels_url":"https://api.github.com/repos/crystal-lang/crystal/issues/6759/labels{/name}","comments_url":"https://api.github.com/repos/crystal-lang/crystal/issues/6759/comments","events_url":"https://api.github.com/repos/crystal-lang/crystal/issues/6759/events","html_url":"https://github.com/crystal-lang/crystal/issues/6759","id":362377963,"node_id":"MDU6SXNzdWUzNjIzNzc5NjM=","number":6759,"title":"100% CPU usage if TCPSocket reads data after it can't connect","user":{"login":"girng","id":36117592,"node_id":"MDQ6VXNlcjM2MTE3NTky","avatar_url":"https://avatars0.githubusercontent.com/u/36117592?v=4","gravatar_id":"","url":"https://api.github.com/users/girng","html_url":"https://github.com/girng","followers_url":"https://api.github.com/users/girng/followers","following_url":"https://api.github.com/users/girng/following{/other_user}","gists_url":"https://api.github.com/users/girng/gists{/gist_id}","starred_url":"https://api.github.com/users/girng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/girng/subscriptions","organizations_url":"https://api.github.com/users/girng/orgs","repos_url":"https://api.github.com/users/girng/repos","events_url":"https://api.github.com/users/girng/events{/privacy}","received_events_url":"https://api.github.com/users/girng/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2018-09-20T21:38:54Z","updated_at":"2018-09-21T14:03:07Z","closed_at":null,"author_association":"NONE","body":"## Issue\r\nThis is really simple to replicate, but I'm terrible at explaining things so please bear with me.\r\n\r\nIf you run server.cr first, then run client.cr it works perfectly! CPU Usage of client.cr is 0-1%.\r\n\r\nHowever, if you run client.cr first, then server.cr, it works.. except there is one catch. The CPU usage for your client.cr in `top` is at constant 100%, **indefinitely**! Even after it made the connection.\r\n\r\nYou might be wondering what's the use case? Well, I have a Master Server and if it goes down and back up, I want my game instance servers to automatically connect and authenticate themselves. It works beautifully, except this part :P \r\n\r\n\r\n`server.cr:`\r\n```\r\nrequire \"socket\"\r\n\r\nIP   = \"0.0.0.0\"\r\nPORT = 9300\r\n\r\nserver = TCPServer.new(IP, PORT)\r\nputs \"Server bound on port #{IP}:#{PORT}\"\r\n\r\ndef handle_connection(socket)\r\n  puts \"New connection! #{socket}\"\r\n  loop do\r\n    payload_size = socket.read_bytes(Int32)\r\n    message = socket.read_string(payload_size)\r\n    puts message\r\n  end\r\nrescue e\r\n  puts e\r\nend\r\n\r\nwhile socket = server.accept?\r\n  spawn handle_connection(socket)\r\nend\r\n```\r\n\r\n`client.cr:`\r\n```\r\nrequire \"socket\"\r\n\r\ndef handle_connection(socket)\r\n  begin\r\n    loop do\r\n      payload_size = socket.read_bytes(Int32)\r\n      puts payload_size\r\n      raw_message = socket.read_string(payload_size)\r\n      puts raw_message\r\n    end\r\n  rescue e\r\n    puts \"Disconnected from the Master Server\"\r\n    puts e\r\n  end\r\nend\r\n\r\ndef gogo\r\n  begin\r\n    puts \"Waiting for the server to become connectable..\"\r\n    check = TCPSocket.new(\"0.0.0.0\", 9300)\r\n    check.tcp_nodelay = true\r\n    puts \"Successfully connected to the Master Server\"\r\n    spawn handle_connection(check)\r\n    loop do\r\n      sleep 1\r\n    end\r\n  rescue e\r\n    puts e\r\n  end\r\nend\r\n\r\ngogo()\r\n```\r\n- Crystal 0.26.1 [391785249] (2018-08-27)\r\n"},"comment":{"url":"https://api.github.com/repos/crystal-lang/crystal/issues/comments/423541956","html_url":"https://github.com/crystal-lang/crystal/issues/6759#issuecomment-423541956","issue_url":"https://api.github.com/repos/crystal-lang/crystal/issues/6759","id":423541956,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMzU0MTk1Ng==","user":{"login":"girng","id":36117592,"node_id":"MDQ6VXNlcjM2MTE3NTky","avatar_url":"https://avatars0.githubusercontent.com/u/36117592?v=4","gravatar_id":"","url":"https://api.github.com/users/girng","html_url":"https://github.com/girng","followers_url":"https://api.github.com/users/girng/followers","following_url":"https://api.github.com/users/girng/following{/other_user}","gists_url":"https://api.github.com/users/girng/gists{/gist_id}","starred_url":"https://api.github.com/users/girng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/girng/subscriptions","organizations_url":"https://api.github.com/users/girng/orgs","repos_url":"https://api.github.com/users/girng/repos","events_url":"https://api.github.com/users/girng/events{/privacy}","received_events_url":"https://api.github.com/users/girng/received_events","type":"User","site_admin":false},"created_at":"2018-09-21T14:03:07Z","updated_at":"2018-09-21T14:03:07Z","author_association":"NONE","body":"\r\n> I give up, no reasoning with you ...\r\n\r\nSee edit. Not IO blocking to warrant 100% CPU usage, that you claimed."}}
{"actor":{"display_login":"girng"}}