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

Use the filelock library to determine the primary GUI/Core process #7660

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

kozlovsky
Copy link
Contributor

@kozlovsky kozlovsky commented Oct 30, 2023

This PR provides an alternative way (as compared to #7637) to determine the primary Tribler process. This PR uses the filelock library for that purpose:

  • Now, when the primary Tribler GUI/Core process is running, the corresponding lock files 'tribler-gui.lock'/'tribler-core.lock' are created and held until the related GUI/Core process is finished.
  • ProcessManager still keeps all Tribler processes in the database but does not determine which process can be primary.
  • The database of processes is still used to pass the opened REST API port from the Core process to the GUI process. The reason for this is that it is still possible that multiple processes of the same type can run in parallel (for example, one primary and several secondary processes), and it is safer to store port in a process-specific place inside the database instead of using a global variable; this way few errors caused by race conditions can happen.

The typical usage of the new API looks the following way:

  1. First, the process tries to acquire the file lock:

    process_lock = try_acquire_file_lock(lock_file_name)
    current_process_has_lock = bool(process_lock)

    If the attempt to grab the lock is successful, the process is considered a primary process; otherwise, it is a secondary process and should terminate after passing the magnet link to the primary process.

    The current process should hold the lock object returned from the try_acquire_file_lock call until the process finishes, as the lock releases if the lock object is garbage collected. The process can do process_lock.release() before it finishes, but it is not necessary, as (1) the object will be garbage collected after the program exits the scope where the lock was declared, and (2) even if the lock file was not removed from the disk, the lock is still considered as released after the current process is finished. On Linux, the lock file is not always deleted at the end because of the details of how Linux works with open file descriptors, but it is not a problem.

  2. The ProcessManager is initialized so the current process (primary or secondary) can store information about itself in the database:

    process_manager = ProcessManager(root_state_dir)
    process_manager.setup_current_process(kind=ProcessKind.Core, has_lock=current_process_has_lock)
    set_global_process_manager(process_manager)
  3. The process should check whether the attempt to grab the lock was successful, and if not, it should terminate:

    if not current_process_has_lock:
        msg = 'Another Core process is already running'
        logger.warning(msg)
        process_manager.sys_exit(1, msg)

So, the API is relatively simple.

@kozlovsky kozlovsky force-pushed the filelock branch 4 times, most recently from fd9d30b to 5174810 Compare November 3, 2023 07:40
@kozlovsky kozlovsky marked this pull request as ready for review November 3, 2023 07:42
@kozlovsky kozlovsky requested review from a team, xoriole and drew2a and removed request for a team November 3, 2023 07:42
@kozlovsky kozlovsky changed the title Use filelock library to determine the primary GUI/Core process Use the filelock library to determine the primary GUI/Core process Nov 3, 2023
Copy link
Contributor

@drew2a drew2a left a comment

Choose a reason for hiding this comment

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

The PR looks good to me.

I have a minor question about the naming and a request for optionally adding descriptions for the tests (otherwise, it can be hard to guess what specific tests are testing).

As a side note, the test coverage for this PR could be increased.

src/tribler/gui/start_gui.py Outdated Show resolved Hide resolved
@kozlovsky kozlovsky requested a review from drew2a November 3, 2023 16:35
@kozlovsky kozlovsky merged commit 719e208 into Tribler:release/7.13 Nov 6, 2023
15 checks passed
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.

2 participants