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

fix(node): Pass NPM_PROCESS_STATE to subprocesses via temp file instead of env var #25896

Merged
merged 14 commits into from
Sep 27, 2024

Conversation

nathanwhit
Copy link
Member

@nathanwhit nathanwhit commented Sep 26, 2024

Fixes #25401. Fixes #25841. Fixes #25891.

I don't love where things are located module-wise, if anyone has strong opinions on where to put some of this code I'll shuffle things around.

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

Sweet.

cli/args/mod.rs Outdated
Comment on lines 760 to 761
// seek to beginning, this file might have been read before
file.seek(std::io::SeekFrom::Start(0)).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Would this actually ever happen? It's a unique fd per processs, right? Maybe we shouldn't bother seeking?

Copy link
Member Author

@nathanwhit nathanwhit Sep 27, 2024

Choose a reason for hiding this comment

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

When the child process inherits the fd, the seek position is shared as well (I spent like an hour debugging why the child was reading an empty string and this was why).

So you have to seek somewhere, you either seek immediately after writing the temp file, or seek before reading. Seemed more reliable to just do it before reading.

It does get reused also with lifecycle scripts - there we make a single tempfile and then it gets inherited by all of the lifecycle script subprocesses

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. Maybe it would be good to say in a comment that the seek is necessary because of the write.

It does get reused also with lifecycle scripts - there we make a single tempfile and then it gets inherited by all of the lifecycle script subprocesses

Is the position synced between the child processes? If so, there might be a race condition here.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is synced between the child processes, but we don't actually run multiple tasks concurrently (though maybe we should, between packages at least?), so it should be safe. I'll add a comment to that effect

cli/args/mod.rs Outdated

// static NPM_PROCESS_STATE: Lazy<Option<NpmProcessState>> = Lazy::new(|| None);
Copy link
Member

Choose a reason for hiding this comment

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

Remove

cli/args/mod.rs Outdated
Comment on lines 762 to 763
file.seek(std::io::SeekFrom::Start(0)).unwrap();
file.read_to_end(&mut buf).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

This will cause panics if anything goes wrong. Maybe we should instead log some error to help ourselves debugging in the future?

let temp_file_fd =
deno_runtime::ops::process::npm_process_state_tempfile(
process_state.as_bytes(),
)?;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add context here?

let process_state = provider.get_npm_process_state();
let fd = npm_process_state_tempfile(process_state.as_bytes())?;
args.env.push((
"DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE_FD".to_string(),
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: maybe make it a static and import it in the CLI?

@nathanwhit nathanwhit merged commit fbddd5a into denoland:main Sep 27, 2024
17 checks passed
@nathanwhit nathanwhit deleted the npm-process-state branch September 27, 2024 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants