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

[Core] Avoid race condition for key generation #3292

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

Michaelvll
Copy link
Collaborator

This is to avoid the racing condition when multiple spot, services or launch run concurrently on a new environemnt. Previously, it is possible that two process generates the public key and private key concurrently causing unmatched keys. For example,

  1. process 1 writes private key
  2. process 2 writes private key
  3. process 2 writes public key
  4. process 1 writes public key

This PR adds a lock for the key generation to avoid this race condition

Tested (run the relevant ones):

  • Code formatting: bash format.sh
  • Any manual or new tests for this PR (please specify below)
  • All smoke tests: pytest tests/test_smoke.py
  • Relevant individual smoke tests: pytest tests/test_smoke.py::test_fill_in_the_name
  • Backward compatibility tests: bash tests/backward_comaptibility_tests.sh

@Michaelvll Michaelvll changed the title [Core] Avoid racing condition for key generation [Core] Avoid race condition for key generation Mar 9, 2024
Copy link
Member

@concretevitamin concretevitamin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @Michaelvll.

@@ -62,6 +63,7 @@
# TODO(zhwu): Support user specified key pair.
PRIVATE_SSH_KEY_PATH = '~/.ssh/sky-key'
PUBLIC_SSH_KEY_PATH = '~/.ssh/sky-key.pub'
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'
# Lock protecting public/private sky-key generation.
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'

Copy link
Member

Choose a reason for hiding this comment

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

How about putting all our generated files in ~/.sky/generated or a lock dir under ~/.sky?

Copy link
Collaborator Author

@Michaelvll Michaelvll Mar 9, 2024

Choose a reason for hiding this comment

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

Good point! Moved it to ~/.sky/generated/ssh and test it in a new environment by running 5 concurrent launch.

for i in `seq 1 5`; do sky launch -c test-gcp-$i -d -y --cloud gcp --cpus 2 echo hi & done

@Michaelvll Michaelvll merged commit 2ca1fdf into master Mar 9, 2024
19 checks passed
@Michaelvll Michaelvll deleted the key-generation-lock branch March 9, 2024 23:54
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.

2 participants