Skip to content

fix: fall back to stderr (not stdout) when log-dir is unwritable#5773

Merged
lpcox merged 2 commits into
mainfrom
copilot/fix-corrupted-stdout-json-channel
May 15, 2026
Merged

fix: fall back to stderr (not stdout) when log-dir is unwritable#5773
lpcox merged 2 commits into
mainfrom
copilot/fix-corrupted-stdout-json-channel

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

When awmg cannot open log files in --log-dir, it was falling back to os.Stdout — corrupting the stdout JSON channel that start_mcp_gateway.cjs parses to get the gateway config. The caller sees a SyntaxError with no actionable error from awmg, since log lines like [2026-...] [INFO] [startup] are valid-looking JSON array starts.

Changes

  • internal/logger/file_logger.gohandleFileLoggerError: os.Stdoutos.Stderr; GetWriter() fallback return: os.Stdoutos.Stderr; fallback warning message updated accordingly
  • internal/logger/common.go — Update doc comment describing the FileLogger fallback strategy
  • internal/logger/file_logger_test.go — Update test subcase and assertion to expect os.Stderr
// Before
func handleFileLoggerError(err error, logDir, fileName string) (*FileLogger, error) {
    logFallbackWarnings(err, "Failed to initialize log file", "Falling back to stdout for logging")
    fl := &FileLogger{..., logger: log.New(os.Stdout, "", 0)}
    return fl, nil
}

// After
func handleFileLoggerError(err error, logDir, fileName string) (*FileLogger, error) {
    logFallbackWarnings(err, "Failed to initialize log file", "Falling back to stderr for logging")
    fl := &FileLogger{..., logger: log.New(os.Stderr, "", 0)}
    return fl, nil
}

When --log-dir points at a directory awmg cannot open log files in,
the FileLogger previously fell back to os.Stdout. Because stdout is
the JSON channel used by start_mcp_gateway.cjs to receive the gateway
configuration, the structured log output was mixed into the JSON payload,
causing:

  SyntaxError: Expected ',' or ']' after array element in JSON at position 5

Change handleFileLoggerError to use os.Stderr and update GetWriter()
to return os.Stderr in fallback mode. Update related comments and tests.
Copilot AI changed the title [WIP] Fix awmg corrupting stdout JSON channel when log-dir is unwritable fix: fall back to stderr (not stdout) when log-dir is unwritable May 15, 2026
Copilot finished work on behalf of lpcox May 15, 2026 22:51
Copilot AI requested a review from lpcox May 15, 2026 22:51
@lpcox lpcox marked this pull request as ready for review May 15, 2026 23:13
Copilot AI review requested due to automatic review settings May 15, 2026 23:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a bug where falling back to os.Stdout for logging when the log directory was unwritable would corrupt the stdout JSON channel parsed by start_mcp_gateway.cjs. The fix switches the fallback to os.Stderr across the FileLogger implementation, its GetWriter method, and updates the related doc comments and test.

Changes:

  • Switch FileLogger fallback writer from os.Stdout to os.Stderr in handleFileLoggerError and GetWriter.
  • Update documentation comments (common.go, file_logger.go) to reflect the new fallback target and rationale.
  • Update test subcase name and assertion in file_logger_test.go to expect os.Stderr.
Show a summary per file
File Description
internal/logger/file_logger.go Switch fallback writer to stderr; update doc comments and warning string.
internal/logger/common.go Update FileLogger fallback strategy doc comment to mention stderr.
internal/logger/file_logger_test.go Update test subcase name and assertion to expect os.Stderr.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 0

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.

awmg silently corrupts stdout JSON channel by writing structured logs to stdout when log-dir isn't writable

3 participants