Skip to content

Refactor append_file to avoid holding global mutex during I/O operations #166

Description

@coderabbitai

Problem

The append_file method in src/interpreter/mod.rs currently holds the global file_handles mutex while performing async I/O operations (seek/write/flush/sync). This blocks all other file operations unnecessarily during the I/O.

Current Implementation

The method performs all I/O operations while holding the lock:

let (_, file) = match file_handles.get_mut(handle_id) {
    Some(entry) => entry,
    None => return Err(format\!("Invalid file handle: {handle_id}")),
};

match AsyncSeekExt::seek(file, std::io::SeekFrom::End(0)).await {
    // ... all I/O operations while holding the lock
}

Proposed Solution

Clone the file handle while holding the lock, then drop the lock before performing I/O:

  1. Get mutable reference to file and clone it while lock is held
  2. Drop the lock immediately after cloning
  3. Perform all async I/O operations on the cloned handle

This pattern should also be applied to other methods like write_file and close_file that have similar issues.

References

Files to Modify

  • src/interpreter/mod.rs - append_file, write_file, close_file methods

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions