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

If HTTP connection closed prematurely, consider retry #16025

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

djn24
Copy link
Contributor

@djn24 djn24 commented Dec 23, 2024

xhr2 (used for HTTP under node) will return response=null if the server returns a response but the connection is closed before the full response has been downloaded. Previously this would lead to a null reference in some handlers, e.g. glTFFileLoader would throw "Cannot read properties of null (reading 'byteLength')". With this change onSuccess is not called if the response is null; instead the retry logic can choose to retry or error.

xhr2 (used for HTTP under node) will return response=null if the server
returns a response but the connection is closed before the full
response has been downloaded. Previously this would lead to a null
reference in some handlers, e.g. glTFFileLoader would throw "Cannot
read properties of null (reading 'byteLength')". With this change
onSuccess is not called if the response is null; instead the retry
logic can choose to retry or error.
@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

1 similar comment
@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

@bjsplat
Copy link
Collaborator

bjsplat commented Dec 23, 2024

@sebavan
Copy link
Member

sebavan commented Dec 30, 2024

Could you share a link to the file in node ?

@djn24
Copy link
Contributor Author

djn24 commented Jan 1, 2025

Do you mean the xhr2.js file? I'm not sure I can easily send a link because the source (https://github.com/pwnall/node-xhr2) uses CoffeeScript which is a language I'm not familiar with. The following commands will get you a copy of the file I'm seeing though:

mkdir t
cd t
npm install [email protected]
vi node_modules/xhr2/lib/xhr2.js

Line numbers there agree with the stack trace I included in the forum post:

innerError: LoadFileError: Cannot read properties of null (reading 'byteLength')
      at /Users/djn/Code/real-space/node/webpack:/real-space/node_modules/@babylonjs/core/Misc/fileTools.js:415:1
      at handleError (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/@babylonjs/core/Misc/fileTools.js:486:1)
      at XMLHttpRequest.onReadyStateChange (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/@babylonjs/core/Misc/fileTools.js:537:1)
      at XMLHttpRequest.dispatchEvent (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/xhr2/lib/xhr2.js:72:1)
      at XMLHttpRequest._setReadyState (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/xhr2/lib/xhr2.js:422:1)
      at XMLHttpRequest._onHttpResponseClose (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/xhr2/lib/xhr2.js:637:1)
      at IncomingMessage.<anonymous> (/Users/djn/Code/real-space/node/webpack:/real-space/node_modules/xhr2/lib/xhr2.js:571:1)
      at IncomingMessage.emit (node:events:514:28)
      at emitCloseNT (node:internal/streams/destroy:132:10)
      at processTicksAndRejections (node:internal/process/task_queues:81:21)

_onHttpResponseClose is the place to look (line 637). It sets the ready state to DONE, which is what triggers the callback into Babylon's fileTools code. A couple of lines above it calls _setError which (among other things) sets response to null. _setError does not change status though. status is set on line 574 of xhr2.js, when the response headers have been received.

If I'm understanding the code correctly: If the response headers are received status will be set, and if the connection is then closed before the whole response is downloaded then the response is set to null, the status is left unchanged but the ready state is set to DONE and the onReadyState callback is called. Babylon checks the status code, sees that it's OK and calls the callback with a null response, which (at least in the case of the gltfLoader) isn't expected and leads to the exception in the stack trace above.

Copy link
Member

@sebavan sebavan left a comment

Choose a reason for hiding this comment

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

I am wondering in this case if the check should be !== null and !== undefined ?

Also it sounds highly specific to the xhr2 implementation. Do you know what the XHR spec says regarding this issue ?

@sebavan
Copy link
Member

sebavan commented Jan 3, 2025

looking at
image

It looks like null check only is fine and it should end up retrying so all good for me.

@sebavan
Copy link
Member

sebavan commented Jan 3, 2025

Wait just noticing that null can be a valid response in the JSON case. I think this check should be added then.

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.

3 participants