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

res.cork(() => res.upgrade()) in Upgrade handler with ws.send() in Open handler = close connection #1108

Open
e3dio opened this issue Oct 2, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@e3dio
Copy link
Contributor

e3dio commented Oct 2, 2024

Doing as described in title can result in websocket dirty close connection, to prevent this can do setTimeout(() => ws.send('msg'), 0) if need to send message in Open handler after async Upgrade. I think calling res.upgrade() while corked calls the Open handler still corked and ws.send inside the upgrade cork can cause some tcp or protocol error. Removing res.cork() fixes connection error but displays warnings that cork is missing

@uNetworkingAB uNetworkingAB added the bug Something isn't working label Oct 2, 2024
@uNetworkingAB
Copy link
Contributor

Do you have a full reproducer?

@e3dio
Copy link
Contributor Author

e3dio commented Oct 4, 2024

Here is example it causes issue like 50% of the time, look for immediate socket close after open

import { App } from 'uWebSockets.js';

const port = 80;

App()

.get('/', r => r.end(`<script>
   ws = new WebSocket('ws'+location.protocol.slice(4,-1)+'://'+location.host+'/ws');
   ws.onopen = e => console.log('open');
   ws.onmessage = e => console.log('message', e.data);
   ws.onclose = e => console.log('close');
</script>`))

.ws('/ws', {
   upgrade: async (res, req, context) => {
      const wsHeaders = [ req.getHeader('sec-websocket-key'), req.getHeader('sec-websocket-protocol'), req.getHeader('sec-websocket-extensions') ];
      res.onAborted(() => res.aborted = true);
      await new Promise(r => setTimeout(r, 100));
      res.cork(() => res.upgrade({}, ...wsHeaders, context));
   },
   open: ws => console.log('open') || ws.send('w'.repeat(56)),
   message: ws => console.log('message'),
   close: ws => console.log('close'),
})

.listen(port, s => s && console.log(`http://localhost:${port}`));

@e3dio
Copy link
Contributor Author

e3dio commented Oct 5, 2024

ws-bad2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants