Skip to content

Ensure Session token remains safe with multiple processes. #236

@tegefaulkes

Description

@tegefaulkes

An MR has been created for this issue here: https://gitlab.com/MatrixAI/Engineering/Polykey/js-polykey/-/merge_requests/212

Specification

The Session Session.writeToken in src/sessions/Session.ts:116 makes use of proper-lockfile. However we need to create a test to make sure this functions properly when multiple processes are attempting to update the token file.

The proper-lockfile library is fairly old, and is more complex than what we require. As such, we should switch to using fd-lock instead.

fd-lock only provides two methods: lock and unlock. There is no explicit method to check whether a file is already locked, however, since a file can only be locked if it is not already locked then we can simply attempt to lock the file - if we fail, the file is already locked.

Additional context

imagine 2 calls

pk dosomething1 # uses T1 token (T1 token changes to T2)
pk dosomething2 # uses T2 token

that's in the case of a serial calls but in parallel calls

pk dosomething1 & # uses T1 token (changes to T2 when it finishes)
pk dosomething2 & # uses T1 token as well 
(tries to change to T3, but drops the change when it realises that T2 locking

so at the end of parallel calls, the token state us still T2, but that's fine as T2 is still valid, and T3 is simply dropped

Information on fd-lock can be found here.

Tasks

  1. Explore the use of fd-lock as an alternative to proper-lockfile
  2. Create test for multiple processes attempting to update session and token file (in tests/client and/or tests/bin).
  3. Confirm that the locking functions as expected.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions