question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling .destroy() when uploading multiple files does not work correctly

See original GitHub issue

This escalated from a question I asked last week (https://github.com/jaydenseric/graphql-upload/issues/151). It’s since been closed and my comment possibly went unnoticed, hence a new issue.

Using:

graphql-upload 8.0.6 apollo-server-express 2.0.5 or 2.6.2 - same behaviour with both, either way most likely not an Apollo issue

Mutation schema:

testUploads(uploads: [Upload]): Boolean

Resolver:

testUploads: (_, { uploads }) => {
  console.log("Mutation called");

  return Promise.all([
    uploads[0].then(file => {
      console.log("Upload 0 resolved");
      const stream = file.createReadStream();
      stream.resume(); // LINE 1
    }),
    uploads[1].then(file => {
      console.log("Upload 1 resolved");
      const stream = file.createReadStream();
      stream.resume(); // LINE 2
    })
  ]).then(() => true);
}

I am calling this code via the Insomnia client, using a manually formed multipart request:

{
  "operationName": null,
  "variables": { "uploads": [null, null] },
  "query": "mutation ($uploads: [Upload]) {  testUploads(uploads: $uploads)  }"
}

{ "0": ["variables.uploads.0"], "1": ["variables.uploads.1"] }

I am uploading two different files, their sizes do not seem to have any influence over this.

Here’s the odd behaviour:

It does not matter what I have in LINE 1 - could be stream.resume(), stream.destroy(), a stream.pipe(process.stdout), or it could be commented out entirely (i.e. a stream is created via createReadStream() but nothing is ever done with it!), this has no influence to how the code works.

Now, LINE 2 can also be just about anything - a stream.resume(), stream.pipe(process.stdout), or commented out. Whatever the combination of LINE 1 and LINE 2, the code appears to work fine, i.e. the mutation gets called each time, the uploads resolve and I get a response with the value true, as expected. I don’t see any increase in the memory usage. Perhaps the unused uploads are piling up somewhere in the filesystem, but right now that’s beyond my concerns.

There is one important exception, however: if LINE 2 is stream.destroy(), then this happens:

  1. The first time I call the mutation everything seems to be working fine - both uploads resolve and I get my response.
  2. However, the second time I call it, the request just hangs - it does not even reach my HTTP loggers. I suspect it doesn’t even hit Express itself, so not sure what is actually going on.
  3. Here’s the fun part. If I cancel the request and then fire another one - it works okay again.
  4. If I then fire a fourth request, you guessed it - it hangs.

From hereon, every other request succeeds, and every other hangs.

The behaviour seems to at least partially defy statements previously made by @mike-marcacci:

  1. The bug only manifests itself when I use stream.destroy() on the second upload, so in some way the order of upload processing (or whatever this is) does matter.
  2. If I do nothing with both streams, there really is no problem, so it doesn’t seem like I have to do anything with the streams at all, even after they have been created via createReadStream().

Turns out a properly working stream.destroy() is more important than I first thought. The new .pipeline() method plus the original pipe library rely on using destroy(). Also, while I could fully work around this problem by dropping .pipeline() and using stream.resume() to waste faulty streams, it’s quite a bit of work + thinking in a complex stream processing system and I believe it may not be too efficient and is not appropriate semantically.

Thank you for your continued work and support!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mike-marcaccicommented, Jun 14, 2019

@juona RE fs-capacitor, both are true. The reason that library exists at all is that it’s possible that your resolvers need to use uploads in a different order than they’re sent in the HTTP payload, and it’s also possible for you to use the same upload variable multiple times in your GraphQL query.

Because of this, we have to potentially buffer whole uploads. Of course, we can’t rely on these fitting into memory, and so temporary files are used instead. If your resolvers are available to consume the uploads as they arrive, they receive the content without waiting for the whole thing to be buffered; it also allows a new read stream to be attached at any point in its lifecycle (which will start by consuming data already buffered to the file). The library is very good about garbage collecting these, and makes sure to use the temporary files directory, so any files that were not removed by the library (due to, say, a power failure) are removed by the OS.

1reaction
juonacommented, Jun 14, 2019

Hello there,

Thank you @mike-marcacci so much for the investigation and the detailed explanation and actions taken. It’s not the first time you had to deal with problems @apollo-server, sorry about that. I was kinda hoping the defect would be somewhere in the upload lib, as seeing how you two work, I could’ve probably expected a quick solution : ]

One more question. You mentioned that with fs-capacitor you buffer the uploads into the file system. You did mean to use the word “buffer”, right? I.e. you are not really saving the whole files to the file system and then reading from them again, you are merely buffering the stream?

I never really gave any thought to this, but now I was wondering why fs-capacitor is not helping mitigate this problem, and that is exactly why, yes? Hope this is not a tremendously stupid question.

P. S. I have already learned to always use my own instance of graphql-upload.

P. P. S. Now I am one of the 0.1% too. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot upload multiple files - javascript - Stack Overflow
I have a small part of my app that allow users to upload 5 different images and as soon as an image has...
Read more >
Update: Rails and Active Storage. The new approach to file ...
Learn how to set up and use Active Storage to upload files in your Rails application, as well as about the new additions...
Read more >
Activity | Android Developers
onPause(), Called when the activity loses foreground state, is no longer focusable or before transition to stopped/hidden or destroyed state.
Read more >
Backbone.js
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable ...
Read more >
Stream | Node.js v19.3.0 Documentation
Calling the writable.end() method signals that no more data will be written to the Writable . The optional chunk and encoding arguments allow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found