-
Notifications
You must be signed in to change notification settings - Fork 51
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
catch connect error #246
catch connect error #246
Conversation
twiggy diff reportDifference in .wasm size before and after this pull request.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -224,7 +228,7 @@ function connect(config: ConnectionConfig, forbidTcp: boolean, forbidWs: boolean | |||
} else { | |||
// TCP | |||
connection.socket.destroyed = true; | |||
connection.socket.inner.then((connec) => connec.close()); | |||
connection.socket.inner.then((connec) => connec?.close()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can connec
actually be null/undefined here? I feel like the only way that this could happen is if reset
was called twice, which would be a smoldot bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I returned null
when Deno.connect
returns error, so added this null-check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, I had to re-read the documentation of Promise.catch
to understand
I'm trying very hard to not lose track of what's happening, which can very easily happen with JavaScript.
Given that reset
should never be called after onConnectionReset
, this should never be null
, so:
connection.socket.inner.then((connec) => connec?.close()); | |
connection.socket.inner.then((connec) => connec!.close()); |
}).catch((error) => { | ||
socket.destroyed = true; | ||
config.onConnectionReset(error.toString()); | ||
return null!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you maybe move this as the second parameter of the then
below?
This would guarantee that the first closure of the then
never gets a null
Also:
}).catch((error) => { | |
socket.destroyed = true; | |
config.onConnectionReset(error.toString()); | |
return null!; | |
}).catch((error) => { | |
socket.destroyed = true; | |
config.onConnectionReset(error.toString()); | |
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That first closure (and .send
too) checks socket.destroyed
, so doesn't use null
.
Either null!
is required, or change of type to TcpConnection { socket(inner: Promise<Deno.TcpConn | null>) }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or change of type to TcpConnection { socket(inner: Promise<Deno.TcpConn | null>) }
Well, yes, that option.
As far as I understand, null!
lies to the type checker. It's an escape hatch for situations where TypeScript isn't good enough, and not something you're supposed to use normally.
I've pushed commits doing the change I've requested, plus a CHANGELOG entry |
Thanks! |
Deno does not allow unhandled promise rejection and terminates with
error: Uncaught (in promise)
.Added
.catch
toDeno.connect
.It calls
config.onConnectionReset
, likeWebSocket
(success is.onopen
(then may.onerror
) then.onclose
, failure is.onerror
then.onclose
).