Skip to content

Commit 07fef56

Browse files
andersktimabbott
authored andcommitted
logging_handlers: Remove STAGING_ERROR_NOTIFICATIONS setting.
Running notify_server_error directly from the logging handler can lead to database queries running in a random context. Among the many potential problems that could cause, one actual problem is a SynchronousOnlyOperation exception when running in an asyncio event loop. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 58d9975 commit 07fef56

File tree

5 files changed

+23
-52
lines changed

5 files changed

+23
-52
lines changed

zerver/lib/error_notify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def zulip_browser_error(report: Dict[str, Any]) -> None:
104104
)
105105

106106

107-
def notify_server_error(report: Dict[str, Any], skip_error_zulip: bool = False) -> None:
107+
def notify_server_error(report: Dict[str, Any]) -> None:
108108
report = defaultdict(lambda: None, report)
109109
email_server_error(report)
110-
if settings.ERROR_BOT and not skip_error_zulip:
110+
if settings.ERROR_BOT:
111111
zulip_server_error(report)
112112

113113

zerver/logging_handlers.py

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,6 @@ def __init__(self) -> None:
105105
def emit(self, record: logging.LogRecord) -> None:
106106
report: Dict[str, Any] = {}
107107

108-
# This parameter determines whether Zulip should attempt to
109-
# send Zulip messages containing the error report. If there's
110-
# syntax that makes the Markdown processor throw an exception,
111-
# we really don't want to send that syntax into a new Zulip
112-
# message in exception handler (that's the stuff of which
113-
# recursive exception loops are made).
114-
#
115-
# We initialize is_markdown_rendering_exception to `True` to
116-
# prevent the infinite loop of Zulip messages by ERROR_BOT if
117-
# the outer try block here throws an exception before we have
118-
# a chance to check the exception for whether it comes from
119-
# the Markdown processor.
120-
is_markdown_rendering_exception = True
121-
122108
try:
123109
report["node"] = platform.node()
124110
report["host"] = platform.node()
@@ -131,9 +117,6 @@ def emit(self, record: logging.LogRecord) -> None:
131117
if record.exc_info:
132118
stack_trace = "".join(traceback.format_exception(*record.exc_info))
133119
message = str(record.exc_info[1])
134-
is_markdown_rendering_exception = record.msg.startswith(
135-
"Exception in Markdown parser"
136-
)
137120
else:
138121
stack_trace = "No stack trace available"
139122
message = record.getMessage()
@@ -142,7 +125,6 @@ def emit(self, record: logging.LogRecord) -> None:
142125
# seem to result in super-long messages
143126
stack_trace = message
144127
message = message.split("\n")[0]
145-
is_markdown_rendering_exception = False
146128
report["stack_trace"] = stack_trace
147129
report["message"] = message
148130

@@ -171,20 +153,13 @@ def emit(self, record: logging.LogRecord) -> None:
171153
)
172154

173155
try:
174-
if settings.STAGING_ERROR_NOTIFICATIONS:
175-
# On staging, process the report directly so it can happen inside this
176-
# try/except to prevent looping
177-
from zerver.lib.error_notify import notify_server_error
178-
179-
notify_server_error(report, is_markdown_rendering_exception)
180-
else:
181-
queue_json_publish(
182-
"error_reports",
183-
dict(
184-
type="server",
185-
report=report,
186-
),
187-
)
156+
queue_json_publish(
157+
"error_reports",
158+
dict(
159+
type="server",
160+
report=report,
161+
),
162+
)
188163
except Exception:
189164
# If this breaks, complain loudly but don't pass the traceback up the stream
190165
# However, we *don't* want to use logging.exception since that could trigger a loop.

zerver/tests/test_logging_handlers.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,20 @@ def get_host_error() -> None:
200200
"zerver.lib.error_notify.notify_server_error", side_effect=Exception("queue error")
201201
):
202202
self.handler.emit(record)
203-
with self.settings(STAGING_ERROR_NOTIFICATIONS=False):
204-
with mock_queue_publish(
205-
"zerver.logging_handlers.queue_json_publish", side_effect=Exception("queue error")
206-
) as m:
207-
with patch("logging.warning") as log_mock:
208-
self.handler.emit(record)
209-
m.assert_called_once()
210-
log_mock.assert_called_once_with(
211-
"Reporting an exception triggered an exception!", exc_info=True
212-
)
213-
with mock_queue_publish("zerver.logging_handlers.queue_json_publish") as m:
214-
with patch("logging.warning") as log_mock:
215-
self.handler.emit(record)
216-
m.assert_called_once()
217-
log_mock.assert_not_called()
203+
with mock_queue_publish(
204+
"zerver.logging_handlers.queue_json_publish", side_effect=Exception("queue error")
205+
) as m:
206+
with patch("logging.warning") as log_mock:
207+
self.handler.emit(record)
208+
m.assert_called_once()
209+
log_mock.assert_called_once_with(
210+
"Reporting an exception triggered an exception!", exc_info=True
211+
)
212+
with mock_queue_publish("zerver.logging_handlers.queue_json_publish") as m:
213+
with patch("logging.warning") as log_mock:
214+
self.handler.emit(record)
215+
m.assert_called_once()
216+
log_mock.assert_not_called()
218217

219218
# Test no exc_info
220219
record.exc_info = None

zproject/default_settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,6 @@ class JwtAuthKey(TypedDict):
403403
# the server.
404404
# TODO: Replace this with a smarter "run on only one server" system.
405405
STAGING = False
406-
# Configuration option for our email/Zulip error reporting.
407-
STAGING_ERROR_NOTIFICATIONS = False
408406

409407
# How long to wait before presence should treat a user as offline.
410408
# TODO: Figure out why this is different from the corresponding

zproject/dev_settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
EMBEDDED_BOTS_ENABLED = True
8080

8181
SAVE_FRONTEND_STACKTRACES = True
82-
STAGING_ERROR_NOTIFICATIONS = True
8382

8483
SYSTEM_ONLY_REALMS: Set[str] = set()
8584
USING_PGROONGA = True

0 commit comments

Comments
 (0)