Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail blocking socket operations loudly #16047

Open
thomasballinger opened this issue Jan 17, 2022 · 5 comments
Open

Fail blocking socket operations loudly #16047

thomasballinger opened this issue Jan 17, 2022 · 5 comments

Comments

@thomasballinger
Copy link
Contributor

Would it make sense to fail blocking socket operations fail with an error code or a JavaScript exception?

@tiran encountered a TypeError: Cannot read property 'stream' of undefined error when calling accept on a blocking socket that was more confusing than Error: accept call on blocking socket is not supported would have been.

@tiran
Copy link
Contributor

tiran commented Jan 17, 2022

The full traceback is:

/python-wasm/cpython/builddir/node/python.js:158
   throw ex;
   ^

TypeError: Cannot read property 'stream' of undefined
    at Object.accept (/python-wasm/cpython/builddir/node/python.js:5354:12)
    at ___syscall_accept4 (/python-wasm/cpython/builddir/node/python.js:5609:31)
    at _emscripten_receive_on_main_thread_js (/python-wasm/cpython/builddir/node/python.js:7065:14)
    at _do_call (<anonymous>:wasm-function[8329]:0x2e657c)
    at emscripten_current_thread_process_queued_calls (<anonymous>:wasm-function[8323]:0x2e5bc8)
    at emscripten_main_thread_process_queued_calls (<anonymous>:wasm-function[8335]:0x2e671b)
    at /python-wasm/cpython/builddir/node/python.js:1321:20
    at Worker.worker.onmessage (/python-wasm/cpython/builddir/node/python.js:1607:5)
    at Worker.<anonymous> (/python-wasm/cpython/builddir/node/python.js:1656:12)
    at Worker.emit (events.js:400:28)
Emitted 'error' event on process instance at:
    at emitUnhandledRejectionOrErr (internal/event_target.js:579:11)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:403:9)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)
console log of the listening socket
<ref *1> {
  family: 2,
  type: 1,
  protocol: 0,
  server: WebSocketServer {
    _events: [Object: null prototype] {
      connection: [Function (anonymous)],
      closed: [Function (anonymous)],
      error: [Function (anonymous)]
    },
    _eventsCount: 3,
    _maxListeners: undefined,
    _server: Server {
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _connections: 0,
      _handle: [TCP],
      _usingWorkers: false,
      _workers: [],
      _unref: false,
      allowHalfOpen: true,
      pauseOnConnect: false,
      httpAllowHalfOpen: false,
      timeout: 0,
      keepAliveTimeout: 5000,
      maxHeadersCount: null,
      headersTimeout: 60000,
      requestTimeout: 0,
      _connectionKey: '4:127.0.0.1:0',
      [Symbol(IncomingMessage)]: [Function: IncomingMessage],
      [Symbol(ServerResponse)]: [Function: ServerResponse],
      [Symbol(kCapture)]: false,
      [Symbol(async_id_symbol)]: 15
    },
    _removeListeners: [Function: removeListeners],
    clients: Set(0) {},
    _shouldEmitClose: false,
    options: {
      maxPayload: 104857600,
      skipUTF8Validation: false,
      perMessageDeflate: false,
      handleProtocols: null,
      clientTracking: true,
      verifyClient: null,
      noServer: false,
      backlog: null,
      server: null,
      host: '127.0.0.1',
      path: null,
      port: 0
    },
    _state: 0,
    [Symbol(kCapture)]: false
  },
  error: null,
  peers: {},
  pending: [],
  recv_queue: [],
  sock_ops: {
    createPeer: [Function: createPeer],
    getPeer: [Function: getPeer],
    addPeer: [Function: addPeer],
    removePeer: [Function: removePeer],
    handlePeerEvents: [Function: handlePeerEvents],
    poll: [Function: poll],
    ioctl: [Function: ioctl],
    close: [Function: close],
    bind: [Function: bind],
    connect: [Function: connect],
    listen: [Function: listen],
    accept: [Function: accept],
    getname: [Function: getname],
    sendmsg: [Function: sendmsg],
    recvmsg: [Function: recvmsg]
  },
  stream: {
    path: 'socket[0]',
    node: FSNode {
      parent: [FSNode],
      mount: [Object],
      mounted: null,
      id: 19,
      name: 'socket[0]',
      mode: 49152,
      node_ops: {},
      stream_ops: {},
      rdev: 0,
      name_next: undefined,
      sock: [Circular *1]
    },
    flags: 2,
    seekable: false,
    stream_ops: {
      poll: [Function: poll],
      ioctl: [Function: ioctl],
      read: [Function: read],
      write: [Function: write],
      close: [Function: close]
    },
    fd: 3
  },
  saddr: '127.0.0.1',
  sport: 0
}

@sbc100
Copy link
Collaborator

sbc100 commented Jan 17, 2022

Looks like a bug in accept, it should surely be checking the a non-empty listensock.pending or the result of the shift here:

var newsock = listensock.pending.shift();
newsock.stream.flags = listensock.stream.flags;
return newsock;

Looks like an easy fix if you have time to write a PR.

@thomasballinger
Copy link
Contributor Author

So a blocking accept call should is fine, it's only an error if it can't be fulfilled synchronously? Sounds good if so.

@sbc100
Copy link
Collaborator

sbc100 commented Jan 17, 2022

Yes, I don't think crash relates specifically to blocking vs non-blocking.

At the same time, I don't see any support in the code there for blocking (but that isn't the proximate cause of the crash).

tiran added a commit to tiran/emscripten that referenced this issue Mar 11, 2022
``accept()`` syscall no longer crashes with error message
``TypeError: Cannot read property 'stream' of undefined`` when sockfs
has no pending listen socket. The syscall now returns ``EINVAL`` on
error.

See: emscripten-core#16047
Signed-off-by: Christian Heimes <christian@python.org>
sbc100 pushed a commit that referenced this issue Mar 11, 2022
``accept()`` syscall no longer crashes with error message
``TypeError: Cannot read property 'stream' of undefined`` when sockfs
has no pending listen socket. The syscall now returns ``EINVAL`` on
error.

See: #16047
Signed-off-by: Christian Heimes <christian@python.org>

* Combine check for server and pending
@RReverser
Copy link
Collaborator

It seems this issue was resolved & can be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants