HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.19k stars 656 forks source link

haxe.io.Error.Blocked vs haxe.io.Error.Blocking #5587

Open romamik opened 8 years ago

romamik commented 8 years ago

Sockets blocking operations in non-blocking mode throw something strange. Documentation says:

http://api.haxe.org/sys/net/Socket.html

setBlocking (b:Bool):Void

Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediatly by throwing a haxe.io.Error.Blocking value.

http://api.haxe.org/haxe/io/Error.html

Blocked

The IO is set into nonblocking mode and some data cannot be read or written

It looks like a typo in documentation: Blocked vs Blocking. But both parts of documentation are correct. Sockets really throws 'Blocking' and Error really only have 'Blocked'.

import haxe.io.Error;
import sys.net.Host;
import sys.net.Socket;

class Main {

    static function main() {
        var socket = new Socket();
        socket.connect(new Host('google.com'), 80);
        socket.setBlocking(false);

        try {
            throw Error.Blocked;
        }
        catch(e:Error) { 
            trace('catched $e'); // trace: Main.hx:17: catched Blocked
        }

        try {
            trace(socket.read());
        }
        catch(e:Dynamic) { 
            trace('catched $e', Reflect.isEnumValue(e), Reflect.isObject(e), Type.getClass(e)); 
            // trace: Main.hx:23: catched Blocking,false,true,String
        }

        try {
            trace(socket.read());
        }
        catch(e:Error) { 
            trace('catched $e'); // never executed, but get unhandled exception:
            //Called from hxcpp::__hxcpp_main
            //Called from Main::main Main.hx line 28
            //Error : Blocking
        }
    }
}
Simn commented 6 years ago

I think this has been discussed/resolved elsewhere, but I can't remember where/how. Please see if we can close this.

ncannasse commented 6 years ago

This seems hxcpp specific, and it calls for proper sys unit tests.

ncannasse commented 6 years ago

@hughsando can you have a look ?

sonygod commented 6 years ago

neko has the same error . https://community.haxe.org/t/socket-was-blocked-need-help/792/2

hughsando commented 6 years ago

So, non-blocking io should throw an instance of Error, the 'Blocked' enum, rather than the "Blocking" string? Seems sensible, and I'm happy to change if that is the official word.

ncannasse commented 6 years ago

Yes, "Blocking" string should not happen at user level.

matrefeytontias commented 3 years ago

Bump, this still happens on C++ :

socket.setBlocking(false);
socket.listen(1);
socket.accept();

Output :

Error : Blocking