Open Bug 2069767 Opened 2 months ago Updated 7 days ago

On Windows, Output() in nsAppRunner.cpp unconditionally shows a MessageBox (even in headless mode, non-interactive sessions, console programs, ...)

Categories

(Toolkit :: Startup and Profile System, defect)

defect

Tracking

()

People

(Reporter: mathew.hodson, Unassigned)

References

(Depends on 1 open bug)

Details

MOZ_WINCONSOLE is used in toolkit/xre/nsAppRunner.cpp to determine how to show an error message. It was never defined for XRE at the Make level as far as I can tell. I think the intention was to allow developers to configure the environment variable themselves when needed.

Since bug 2063823, the only way to define MOZ_WINCONSOLE is to set WINCONSOLE in the moz.build file, which isn't done for XRE so it seems like that code path is dead.

browser/app/nsBrowserApp.cpp uses MOZ_WINCONSOLE in a similar way to show an error message. Should we set WINCONSOLE in toolkit/xre/moz.build like it is set in browser/app/moz.build?

if CONFIG["MOZ_ASAN"] or CONFIG["MOZ_DEBUG"]:
    WINCONSOLE = True
else:
    WINCONSOLE = False

Probably not a build system question, so I'll move it.

For what it's worth, Claude suggests:

In Output(), replace the #if defined(XP_WIN) && !MOZ_WINCONSOLE branch with a plain #if defined(XP_WIN) branch that checks whether stderr is usable, roughly:

#if defined(XP_WIN)
  HANDLE stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
  if (stderrHandle && stderrHandle != INVALID_HANDLE_VALUE) {
    vfprintf(stderr, fmt, ap);
  } else {
    // existing Vsmprintf + MessageBoxW code, unchanged
  }
#elif defined(MOZ_WIDGET_ANDROID)

Behavior after the change: console subsystem hosts, redirected stderr, CI harness pipes, and --attach-console (UseParentConsole calls SetStdHandle) all print to stderr. Release firefox.exe started from Explorer keeps the message box. libxul stops referencing MOZ_WINCONSOLE at all.

Component: General → Startup and Profile System
Product: Firefox Build System → Toolkit

I agree that the #else code path is currently dead code on Windows. We always show a message box.

Code in libxul shouldn't reference MOZ_WINCONSOLE at all. Setting WINCONSOLE in toolkit/xre/moz.build like in comment 0 wouldn't be a correct fix. A process uses a console or GUI subsystem based on how the running executable was compiled (e.g. firefox.exe). Being a library, xul.dll can't decide this; it can be loaded into both console and GUI programs. For example, firefox.exe is console in debug or ASan builds, whereas xpcshell.exe is always console, and they both use xul.dll. Copying the browser/app condition would match firefox.exe, but be wrong for xpcshell.exe in opt builds.

It feels important to me to clarify that this is a highly sensitive code path. It decides how startup errors are shown to the user. If we get things wrong, Firefox could end up not showing anything to users when these failures occur. Example messages:

  • Firefox is already running, but is not responding. To use Firefox, you must first close the existing Firefox process, restart your device, or use a different profile.
  • This profile was last used with a newer version of this application. Please create a new profile.
  • Error: Platform version '<v>' is not compatible with minVersion >= <a> maxVersion <= <b> Maybe try to reinstall Firefox?

These are important hints we give users to help them fix their setup, it's also what our support pages expect users to see when things go wrong. So we must make sure that the messages reach the user in all scenarios. If we output them to stderr, we must be confident that it's only in scenarios where the user will actually expect the error to be in stderr and read it there. The suggestion from comment 1 looks dangerous in this regard, because it assumes that a stderr handle existing is enough to ensure that an error we put there will be found and read by the user.

I found related issues that suggest that this is a concrete defect (at minimum in headless mode and/or non-interactive sessions):

  • two occurrences of // TODO: make a way to turn off all dialogs when headless. in nsBrowserApp.cpp;
  • bug 2062988 makes us call Output to bypass a dialog in headless mode, but Output does generate a dialog on Windows (this was most likely tested on macOS);
  • Firefox doesn't work in non-interactive sessions (e.g. bug 1398618);
  • also background task mode resorts to printf_stderr to avoid Output code paths, which looks like a workaround for the current bug.

Updating the bug title and classification accordingly.

Type: task → defect
See Also: → 2091523, 1398618, 2062988
Summary: Should toolkit/xre/moz.build set WINCONSOLE → On Windows, Output() in nsAppRunner.cpp unconditionally shows a MessageBox (even in headless mode, non-interactive sessions, console programs, ...)
Depends on: 2094487
You need to log in before you can comment on or make changes to this bug.