Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Error: EBUSY: resource busy or locked on native modules #1589

Closed

Description

@mertushka

What version of pkg are you using?

5.6.0

Which version(s) of pkg work for you?

< 5.6.0

What version of Node.js are you using?

16.14.0

What operating system are you using?

Windows

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

node14-win-x64

Describe the Bug

unknown

Expected Behavior

Native modules must import succesfully

To Reproduce

Idk.

Activity

  1. marcinadrem commented on Apr 22, 2022

    @marcinadrem

    I experienced the same behavior when executing multiple instances of packed executable. It seems like native module is locked in "FILE LOCKED WITH ONLY READERS" mode and second instance tries to open it in write mode. Please see attached log.

    Logfile.CSV

    Please unpack attached file and run run_test.bat to reproduce

    load_native_module_test.zip

  2. renkei commented on Apr 25, 2022

    @renkei
    Contributor

    I'm not sure but this could be related to PR1492 in 5.6.0. The .node files are always copied now to make sure that the temp folder contains all related files, see PR for details. But if there is an already running instance the native module files are already opened and therefore cannot be overwritten. Solution could be to perform always the copy action, just to make sure that the tmp folder is always properly populated, but to allow to fail. Then the second instance of the pkg app should just proceed with the already existing file.

    For instance, copyFolderRecursiveSync in line 183 of prelude/bootstrap.js could be modified like this:

    function copyFolderRecursiveSync(source, target) {
      let files = [];
    
      // Check if folder needs to be created or integrated
      const targetFolder = path.join(target, path.basename(source));
      if (!fs.existsSync(targetFolder)) {
        fs.mkdirSync(targetFolder);
      }
    
      // Copy
      if (fs.lstatSync(source).isDirectory()) {
        files = fs.readdirSync(source);
        files.forEach((file) => {
          const curSource = path.join(source, file);
          if (fs.lstatSync(curSource).isDirectory()) {
            copyFolderRecursiveSync(curSource, targetFolder);
          } else {
            try {
              fs.copyFileSync(
                curSource,
                path.join(targetFolder, path.basename(curSource))
              );
            } catch (err) {
              // copyInChunks inside copyFileSync failed,
              // let us know, but proceed with next file.
              console.error(err)
            }
          }
        });
      }
    }

    I'm not familiar with the code base, so please feel free to correct me or to point me to another solution. I can also provide a PR if this is suggested.

  3. renkei commented on Apr 26, 2022

    @renkei
    Contributor

    In general, the app start takes longer now since everything is always copied. Apps build with pkg 5.5.2 starts much faster the second time. Maybe it is a better idea in contrast to my suggestion above (try to copy but allow to fail if the file is already in use) to check before if the file already exists and if so to skip the copy.

    This should guarantee that the tmp folder is always properly populated while it also speeds up the app start like in pkg 5.5.2 if all files are already there (best case).

  4. luckyyyyy commented on Apr 28, 2022

    @luckyyyyy

    same problem here, Multiple processes create files, thread unsafe

  5. robertsLando commented on Apr 29, 2022

    @robertsLando
    Contributor

    Anyone that wants to create a PR to fix this? The part of code that creates the problem is identified so it's just a matter of doing some tests

  6. mertushka commented on Apr 29, 2022

    @mertushka
    Author

    @renkei Can you test the code you wrote on the local? If everything is good then you would make pr.

  7. renkei commented on Apr 29, 2022

    @renkei
    Contributor

    I will try to create a PR over the weekend.

  8. added a commit that references this issue on May 1, 2022
  9. renkei commented on May 1, 2022

    @renkei
    Contributor

    PR is available now, I've tested this PR with my pkg-packaged app that uses .node files. Works fine again.

    The PR has two effects:

    1. .node files that must exist on the real filesystem instead of the virtual snapshot filesystem (because of process.dlopen) are only copied if they do not exist. If they already exist then it doesn't matter anymore whether they are currently opened by an running instance of the pkg-packaged app or not, because existing files are untouched now.
    2. The start of the pkg app a second, third, etc. time is as fast as with pkg 5.5.2 again since (in best case) no copy action takes place.

    But in case of missing .node files in the tmp folder the missing files are copied again, even if the folder structure is still existing. This makes PR1492 still relevant and important. Sometimes (at least on Windows) the folder structure inside the tmp folder was still there but the .node files were gone.

    Please review, if everything is OK for you I'm looking forward to see pkg 5.6.1 ;-)

  10. added a commit that references this issue on May 9, 2022
  11. added a commit that references this issue on May 9, 2022
    f0c4e8c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions