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

The allowHalfOpen doesn't seem to be working on the server #41

Open
CMCDragonkai opened this issue May 13, 2021 · 9 comments
Open

The allowHalfOpen doesn't seem to be working on the server #41

CMCDragonkai opened this issue May 13, 2021 · 9 comments

Comments

@CMCDragonkai
Copy link

I've created a server like:

    const utpSocket = UTP.createServer(
      async (utpConn) => {
        utpConn.on('close', () => {
          console.log('CLOSED ON REMOTE SIDE');
        });
      },
      {
        allowHalfOpen: false
      }
    );

Using the allowHalfOpen I'm expecting that the utpConn would be closed if the remote side has sent an end event. That is they closed their write side, ending my read side. Therefore I should have an automatic close of my write side, and that should trigger a close event.

However that's not what is happening. Instead I get a UTPCONNRESET error.

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (Error: UTP_ECONNRESET
    at createUTPError (/home/cmcdragonkai/Projects/js-polykey/node_modules/utp-native/lib/connection.js:238:15)
    at Connection.Object.<anonymous>.Connection._onerror (/home/cmcdragonkai/Projects/js-polykey/node_modules/utp-native/lib/connection.js:175:16) {
  code: 'UTP_ECONNRESET',
  errno: 1
})
    at Connection.emit (events.js:304:17)
    at Connection.Object.<anonymous>.Connection._onclose (/home/cmcdragonkai/Projects/js-polykey/node_modules/utp-native/lib/connection.js:169:25) {
  code: 'ERR_UNHANDLED_ERROR',
  context: Error: UTP_ECONNRESET
      at createUTPError (/home/cmcdragonkai/Projects/js-polykey/node_modules/utp-native/lib/connection.js:238:15)
      at Connection.Object.<anonymous>.Connection._onerror (/home/cmcdragonkai/Projects/js-polykey/node_modules/utp-native/lib/connection.js:175:16) {
    code: 'UTP_ECONNRESET',
    errno: 1
  }
}

To actually ensure that my side is being closed if this occurs, I have to attach my own event handler to the end:

        utpConn.on('end', () => {
          utpConn.end();
        });

Only then does the connection actually close.

@mafintosh
Copy link
Owner

Sounds like a bug, we only use halfOpen mode so prob why we haven't noticed then. Can you PR a test case, then we'll get it fixed.

@CMCDragonkai
Copy link
Author

Ok I have discovered a strange situation.

The end event only gets received IF I attach data event handler.

If the data event handler doesn't get attached, the end event is never fired.

@CMCDragonkai
Copy link
Author

So to get the behaviour I'm lookign for:

  1. I need to have data handler
  2. I need to have end handler that runs the end

It just seems that allowHalfOpen on the server side doesn't do anything at all.

But the fact that I also need a data handler seems like a bug as well.

@mafintosh
Copy link
Owner

Yea that's a stream mechanic. In only goes into flowing mode once you either pipe it, attach a data handler or call resume. That's the same with any node stream

@CMCDragonkai
Copy link
Author

I had no idea that this would have a side-effect:

        utpConn.on('data', (d) => {});

That seems quite confusing. That without attaching a data handler, no end event would ever be received.

@mafintosh
Copy link
Owner

Yes, see my above comment, that's how node.js streams work.

@CMCDragonkai
Copy link
Author

I don't think this is actually resolved. I haven't submitted a PR for this problem.

@mafintosh mafintosh reopened this May 21, 2021
@mafintosh
Copy link
Owner

👍👌

@CMCDragonkai
Copy link
Author

I found that in the code, the allowHalfOpen actually has to be set as the first parameter:

    const utpSocket = UTP.createServer(
      {
        allowHalfOpen: false,
      },
      this.handleConnection
    );

While this means that console.log(utpSocket._allowHalfOpen) does show up as false here.

When you get the utp connection inside handleConnection, the resulting allowHalfOpen property is actually still true.

I've also found that if you try to end it when receiving an end event, it will come up with an error [ERR_STREAM_WRITE_AFTER_END]: write after end. Which seems to go against what we know about allowHalfOpen. Maybe regardless of what I set as the setting, the allowHalfOpen is effectively false.

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

No branches or pull requests

2 participants