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

feat(fetch) AbortSignal #2019

Merged
merged 25 commits into from
Feb 15, 2023
Merged

feat(fetch) AbortSignal #2019

merged 25 commits into from
Feb 15, 2023

Conversation

cirospaciari
Copy link
Collaborator

@cirospaciari cirospaciari commented Feb 9, 2023

  • onData abortion
  • onWritable abortion
  • addEventListener
  • fail with the right reason
  • detect TimeoutError and AbortError
  • fix socket timeout to use TimeoutError
  • add tests

@cirospaciari cirospaciari marked this pull request as ready for review February 15, 2023 01:42
@cirospaciari cirospaciari changed the title WIP feat(fetch) AbortSignal feat(fetch) AbortSignal Feb 15, 2023
@@ -688,6 +688,9 @@ pub const Fetch = struct {
}

pub fn onReject(this: *FetchTasklet) JSValue {
if (this.result.exception) |exception| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the memory still get freed?

Copy link
Collaborator Author

@cirospaciari cirospaciari Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is pretty similar to toErrorInstance from SystemError, I moved all logic to onReject (the default fail SystemError is created here to using toErrorInstance) so I think we don't have any memory leaks here

**anyopaque,
NewHTTPContext(is_ssl).ActiveSocket.init(&dead_socket).ptr(),
);
if (this.globalThis) |globalThis| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's threadsafe to create JSValue here or for it to be aware of globalThis

I think it will need to enqueue a task to the event loop and to do this on the main thread

Copy link
Collaborator Author

@cirospaciari cirospaciari Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this to onReject which is called using task.javascript_vm.eventLoop().enqueueTaskConcurrent so will be threadsafe, and also I removed any reference to globalThis from http_client_async

@@ -2147,6 +2255,7 @@ pub const HTTPClientResult = struct {
fail: anyerror = error.NoError,
redirected: bool = false,
headers_buf: []picohttp.Header = &.{},
exception: ?JSC.JSValue = null,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the exception value itself will have to be part of the code that dispatches fetch instead of living here

Copy link
Collaborator Author

@cirospaciari cirospaciari Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this, the reason is already the exception I still pass using result because I need it in FetchTasklet

Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting closer but we need more tests

We also need a test with addEventListener

beforeAll(() => {
server = Bun.serve({
async fetch(){
await Bun.sleep(2000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot more we should test here

  • what happens when you use ReadableStream and it's aborted?
  • does request.signal.addListener work?
  • what happens when you call request.signal.abort() but the request has already been responded to (does it crash?)
  • what happens when you use ReadableStream type: "direct" and it's aborted?
  • what happens when you use ReadableStream, you call stream.cancel() and then request.signal? Does it crash?

Later we should make Bun.spawn work with AbortSignal as well

Copy link
Collaborator Author

@cirospaciari cirospaciari Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working in the Request object signal and next I can make Bun.spawn, I will write those tests ASAP, you are right my tests are not enough, we need a more complete test suit.

Request object will affect fetch and maybe Bun.server (Force close connection)?

@Jarred-Sumner Jarred-Sumner merged commit 597053e into main Feb 15, 2023
@Jarred-Sumner Jarred-Sumner deleted the ciro/fetch-abort branch February 15, 2023 22:20
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

Successfully merging this pull request may close these issues.

2 participants