Bug 1615921 - Skip writing to pdf file if shutting down r=jwatt

We are intending to advance the toolkit.shutdown.lateWriteChecksStage
pref, to collect information (and crash on DEBUG) about writes which
happen during and after the xpcom-shutdown-threads notification. This
is producing failures in the PrintTargetPDF.cpp destructor because
we end up calling write_func and writing to (I presume) the target
PDF. This _feels_ like something we can just skip, so that's the
review request I am sending, but please let me know if it's critical
that we write to this file during shutdown.

Differential Revision: https://phabricator.services.mozilla.com/D63218

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Doug Thayer 2020-02-24 17:54:09 +00:00
parent f6951a7a52
commit b405df3a9a
3 changed files with 15 additions and 2 deletions

View File

@ -7,11 +7,15 @@
#include "cairo.h"
#include "cairo-pdf.h"
#include "mozilla/AppShutdown.h"
namespace mozilla::gfx {
static cairo_status_t write_func(void* closure, const unsigned char* data,
unsigned int length) {
if (AppShutdown::IsShuttingDown()) {
return CAIRO_STATUS_SUCCESS;
}
nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
do {
uint32_t wrote = 0;
@ -61,8 +65,11 @@ nsresult PrintTargetPDF::EndPage() {
}
void PrintTargetPDF::Finish() {
if (mIsFinished) {
return; // We don't want to call Close() on mStream more than once
if (mIsFinished || AppShutdown::IsShuttingDown()) {
// We don't want to call Close() on mStream more than once, and we don't
// want to block shutdown if for some reason the user shuts down the
// browser mid print.
return;
}
PrintTarget::Finish();
mStream->Close();

View File

@ -30,6 +30,7 @@ namespace mozilla {
static ShutdownPhase sFastShutdownPhase = ShutdownPhase::NotInShutdown;
static ShutdownPhase sLateWriteChecksPhase = ShutdownPhase::NotInShutdown;
static AppShutdownMode sShutdownMode = AppShutdownMode::Normal;
static bool sIsShuttingDown = false;
// These environment variable strings are all deliberately copied and leaked
// due to requirements of PR_SetEnv and similar.
@ -55,6 +56,8 @@ ShutdownPhase GetShutdownPhaseFromPrefValue(int32_t aPrefValue) {
return ShutdownPhase::NotInShutdown;
}
bool AppShutdown::IsShuttingDown() { return sIsShuttingDown; }
void AppShutdown::SaveEnvVarsForPotentialRestart() {
const char* s = PR_GetEnv("XUL_APP_FILE");
if (s) {
@ -156,6 +159,7 @@ void AppShutdown::MaybeFastShutdown(ShutdownPhase aPhase) {
}
void AppShutdown::OnShutdownConfirmed() {
sIsShuttingDown = true;
// If we're restarting, we need to save environment variables correctly
// while everything is still alive to do so.
if (sShutdownMode == AppShutdownMode::Restart) {

View File

@ -18,6 +18,8 @@ enum class AppShutdownMode {
class AppShutdown {
public:
static bool IsShuttingDown();
/**
* Save environment variables that we might need if the app initiates a
* restart later in its lifecycle.