Skip to content

Bug: os.chdir() is process-wide and not thread-safe #568

@gkorland

Description

@gkorland

Description

In api/project.py, os.chdir() is called to change the working directory:

def process_git_history(self, ignore: Optional[List[str]] = []) -> GitGraph:
    original_dir = Path.cwd()
    os.chdir(self.path)           # line ~104
    git_graph = build_commit_graph(self.path, self.analyzer, self.name, ignore)
    os.chdir(original_dir)        # line ~110
    return git_graph

os.chdir() changes the CWD for the entire process. In a multi-threaded web server, concurrent requests will interfere with each other. Additionally, if an exception occurs between the two chdir calls, the directory is never restored.

Impact

  • Concurrent requests can corrupt each other's working directory
  • An exception leaves the process in an unexpected CWD permanently

Suggested Fix

Use subprocess(cwd=...) or pathlib with absolute paths instead of changing global process state. If os.chdir() is unavoidable, use a try/finally block at minimum.

Context

Found during code review of PR #522.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions