Skip to content

fix deadlock error when pipe ends while prompting password#194

Merged
tegefaulkes merged 3 commits intostagingfrom
feature-eng-148-undefined-behaviour-when-piping
Jun 12, 2024
Merged

fix deadlock error when pipe ends while prompting password#194
tegefaulkes merged 3 commits intostagingfrom
feature-eng-148-undefined-behaviour-when-piping

Conversation

@tegefaulkes
Copy link
Contributor

@tegefaulkes tegefaulkes commented Jun 5, 2024

Description

This PR addresses the promise deadlock error when piping into the agent start command with fresh state.

I've applied a fix, however some tests are failing now with new behaviour. So I'll need to look deeper into this. It may just be pkExpect breaking but some concurrent X tests are failing too.

Issues Fixed

Tasks

  • 1. Update the prompt utility functions to return undefined when the stdin ends while awaiting the prompt.
  • 2. Make sure all tests are working

Final checklist

  • Domain specific tests
  • Full tests
  • Updated inline-comment documentation
  • Lint fixed
  • Squash and rebased
  • Sanity check the final build

@linear
Copy link

linear bot commented Jun 5, 2024

@tegefaulkes tegefaulkes self-assigned this Jun 5, 2024
@tegefaulkes tegefaulkes force-pushed the feature-eng-148-undefined-behaviour-when-piping branch from 025c43c to a1c2627 Compare June 6, 2024 01:52
@tegefaulkes
Copy link
Contributor Author

Minor logic bug. It's fixed now. Prepping for merge.

@tegefaulkes tegefaulkes marked this pull request as ready for review June 6, 2024 01:59
@tegefaulkes
Copy link
Contributor Author

This is the simplest and best solution I can think of right now. The core of the problem is that the process.stdin functions differently when piping into the node program. In such a way that prompts doesn't handle it properly.

Prompts fails to process this in two ways

  1. A prompt takes a line as an input, but with the pipe wit will take the full stream as the first line. So if we piped password\npassword, it gets passwordpassword in the first prompt.
  2. It can't handle the end of stream properly. So once the pipe has fully dumped it's data it keeps waiting for input. Resulting in a promise deadlock.

Initially I tried to solve this by creating a promise to track the end of the stdin stream. I could race the prompt with this to prevent the deadlock. but this results with a leaked prompts promise. Also the input from this isn't usable anyway.

The simplest solution is to not prompt at all when handling a pipe. The pipe still works, we just can't function interactively while doing it.

I looked into using the ttys library https://github.com/TooTallNate/ttys/blob/master/index.js. With this we can have the pipe but also the prompt still functions interactively. The library is simple so we can inline the code. However I'm not sure if it's very portable since it's trying to open a /dev/tty with fs.openSync('/dev/tty', 'r').

The only other solution I can think of right now is to detect the piping and have our own stream processor to extract the provided password. Similar to https://stackoverflow.com/questions/59308117/how-to-differentiate-between-standard-inputs-via-pipe-and-via-prompt-in-nodejs

@tegefaulkes
Copy link
Contributor Author

I'm putting this on hold for now pending some discussion.

@CMCDragonkai
Copy link
Member

@tegefaulkes
Copy link
Contributor Author

We're going to go with the current behaviour I've implemented. Where we handle the pipe as if the user cancelled the password prompt.

@tegefaulkes
Copy link
Contributor Author

I had to remove the tests using nExpect to check for prompting. It seems to be using a pipe to function so it's incompatible with the fix.

@tegefaulkes tegefaulkes force-pushed the feature-eng-148-undefined-behaviour-when-piping branch from 3de3de1 to 010b3b8 Compare June 12, 2024 04:07
@tegefaulkes
Copy link
Contributor Author

Ready to merge.

nexpect uses a pipe to function it seems. It is incompatible with the fix. It's weird that it worked before but.

[ci skip]
@tegefaulkes tegefaulkes force-pushed the feature-eng-148-undefined-behaviour-when-piping branch from 010b3b8 to 62c43a7 Compare June 12, 2024 07:46
@tegefaulkes tegefaulkes merged commit 66d8b9d into staging Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Undefined behaviour when piping

2 participants