Skip to content

Commit

Permalink
fix segfault when emitting log messages from fread
Browse files Browse the repository at this point in the history
  • Loading branch information
st-pasha committed Nov 21, 2023
1 parent 2399a37 commit f248a8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/core/read/parallel_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ void ParallelReader::read_all()
if (preframe.nrows_written() < g.max_nrows) {
xassert(end_of_last_chunk == input_end);
}
g.logger_.emit_pending_messages();
}


Expand Down
30 changes: 26 additions & 4 deletions src/core/utils/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Message::Message(Logger* logger, bool warn)

Message::~Message() {
try {
logger_->emit(std::move(out_).str(), emit_as_warning_);
logger_->add(std::move(out_).str(), emit_as_warning_);
}
catch (...) {
std::cerr << "unable to emit log message\n";
Expand Down Expand Up @@ -236,7 +236,7 @@ void Logger::use_pylogger(py::oobj logger) {

Section Logger::section(std::string title) {
if (enabled_) {
emit(std::move(title), false);
add(std::move(title), false);
prefix_ += " ";
}
return Section(this);
Expand Down Expand Up @@ -285,10 +285,32 @@ void Logger::end_section() noexcept {
}


void Logger::emit(std::string&& msg, bool warning) {
// std::lock_guard<std::mutex> pylock(dt::python_mutex());
// This function must be called from the main thread
void Logger::emit_pending_messages() {
xassert(dt::this_thread_index() == 0);
if (!pending_messages_.empty()) {
for (size_t i = 0; i < pending_messages_.size(); i++) {
auto msg = std::move(pending_messages_[i]);
emit_(std::move(msg.first), msg.second);
}
pending_messages_.clear();
}
}


void Logger::add(std::string&& msg, bool is_warning) {
PythonLock pylock;
CallLoggerLock loglock;
if (dt::this_thread_index() == 0) {
emit_pending_messages();
emit_(std::move(msg), is_warning);
} else {
pending_messages_.push_back(std::make_pair(std::move(msg), is_warning));
}
}


void Logger::emit_(std::string&& msg, bool warning) {
// Use user-defined logger object
if (pylogger_) {
HidePythonError hpe;
Expand Down
9 changes: 8 additions & 1 deletion src/core/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#define dt_UTILS_LOGGER_h
#include <iomanip>
#include <sstream> // std::ostringstream
#include <string>
#include <utility>
#include <vector>
#include "_dt.h"
namespace py { class DefaultLogger; }
namespace dt {
Expand Down Expand Up @@ -72,6 +75,7 @@ class Logger {
private:
py::oobj pylogger_;
std::string prefix_;
std::vector<std::pair<std::string, bool>> pending_messages_;
bool enabled_;
bool use_colors_;
size_t : 48;
Expand All @@ -98,9 +102,12 @@ class Logger {
// "python" -> returns the stored python logger object
py::oobj get_pylogger(bool fallback_to_default = true) const;

void emit_pending_messages();

private:
void end_section() noexcept;
void emit(std::string&& msg, bool as_warning);
void add(std::string&& msg, bool as_warning);
void emit_(std::string&& msg, bool as_warning);

friend class Section;
friend class Message;
Expand Down

0 comments on commit f248a8c

Please sign in to comment.