Skip to content

Remove file locking from gateway.log to enable concurrent read access#146

Merged
pelikhan merged 3 commits into
mainfrom
copilot/update-gateway-log-accessibility
Jan 11, 2026
Merged

Remove file locking from gateway.log to enable concurrent read access#146
pelikhan merged 3 commits into
mainfrom
copilot/update-gateway-log-accessibility

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 11, 2026

The gateway.log file was using syscall.Flock with a shared lock, which could interfere with external processes attempting to read the log file. Additionally, buffered writes were not being flushed immediately, delaying log visibility.

Changes

  • Removed file locking: Eliminated all syscall.Flock calls and the locked field from FileLogger
  • Added immediate flushing: Call file.Sync() after each log write to ensure data is immediately visible
  • Simplified Close(): Sync buffered data before closing, removed lock release logic
  • Added integration test: Added TestBinaryInvocation_LogFileCreation to validate log file creation, permissions (0644), concurrent readability, startup message content, and immediate flushing behavior

Implementation

func (fl *FileLogger) Log(level LogLevel, category, format string, args ...interface{}) {
    // ... write log line ...
    
    // Flush immediately to ensure external processes can read
    if fl.logFile != nil {
        if err := fl.logFile.Sync(); err != nil {
            log.Printf("WARNING: Failed to sync log file: %v", err)
        }
    }
}

The file continues to use 0644 permissions, making it readable by all users without special access requirements.

Testing

  • ✅ All unit tests pass (7/7 logger tests)
  • ✅ All integration tests pass (17/17 tests including new log file test)
  • ✅ New test validates log file creation, permissions, concurrent access, and immediate flushing
  • ✅ Build and lint checks pass
Original prompt

Review how the gateway.log is opened so that it can be read by other processes.

  • make sure the log is flushed to file often
  • must not retain lock and not have special permissions
  • must be readable by other users

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Remove file locking (syscall.Flock) to avoid lock interference
- Add explicit Sync() calls after each log write for immediate flushing
- Update Close() to sync before closing
- Remove syscall import (no longer needed)
- Update tests to verify readability and flushing behavior
- File permissions remain 0644 (readable by all users)

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Review gateway.log opening for process readability Remove file locking from gateway.log to enable concurrent read access Jan 11, 2026
Copilot AI requested a review from pelikhan January 11, 2026 08:46
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot add test that do a run Md check creation of log file

- Test verifies mcp-gateway.log is created when binary runs
- Checks log file has correct permissions (0644)
- Validates log file is readable by other processes concurrently
- Confirms log file contains expected startup messages
- Verifies immediate flushing behavior

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 11, 2026

@copilot add test that do a run Md check creation of log file

Added TestBinaryInvocation_LogFileCreation that runs the binary and validates:

  • Log file is created at specified --log-dir location
  • File permissions are 0644 (readable by all)
  • File is readable by other processes while gateway is running
  • Content includes expected startup messages
  • Immediate flushing works correctly

Test passes with all 17 integration tests ✓

Commit: 2e81caa

@pelikhan pelikhan marked this pull request as ready for review January 11, 2026 08:55
@pelikhan pelikhan merged commit ac33d64 into main Jan 11, 2026
3 checks passed
@pelikhan pelikhan deleted the copilot/update-gateway-log-accessibility branch January 11, 2026 08:56
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