Skip to content

Commit 795f3fd

Browse files
bretambroseBret Ambrose
andauthored
Improve log init (#686)
Prevent some crashes when misusing the logging setup API. It is still not safe to multi-init from different threads, but this is an incremental improvement over the previous state. Co-authored-by: Bret Ambrose <bambrose@amazon.com>
1 parent c4a41ab commit 795f3fd

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

source/module.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ static bool s_logger_init = false;
3939
PyObject *aws_py_init_logging(PyObject *self, PyObject *args) {
4040
(void)self;
4141

42-
if (s_logger_init) {
43-
aws_logger_set(NULL);
44-
aws_logger_clean_up(&s_logger);
45-
}
46-
47-
s_logger_init = true;
48-
4942
/* NOTE: We are NOT using aws_py_get_allocator() for logging.
5043
* This avoid deadlock during aws_mem_tracer_dump() */
5144
struct aws_allocator *allocator = aws_default_allocator();
@@ -77,8 +70,18 @@ PyObject *aws_py_init_logging(PyObject *self, PyObject *args) {
7770
log_options.filename = file_path;
7871
}
7972

80-
aws_logger_init_standard(&s_logger, allocator, &log_options);
81-
aws_logger_set(&s_logger);
73+
/* It is not safe to clean up a running logger */
74+
if (s_logger_init) {
75+
aws_raise_error(AWS_ERROR_INVALID_STATE);
76+
return PyErr_AwsLastError();
77+
}
78+
79+
if (aws_logger_init_standard(&s_logger, allocator, &log_options) == AWS_OP_SUCCESS) {
80+
aws_logger_set(&s_logger);
81+
s_logger_init = true;
82+
} else {
83+
return PyErr_AwsLastError();
84+
}
8285

8386
Py_RETURN_NONE;
8487
}

0 commit comments

Comments
 (0)