Files
kte/RegexGuard.cc
T
Claude fb50a02f6a Swap ownership, fsync race, RowsView memory, renderer regex stack
- A session never unlinks a journal it does not own: Detach, ResetJournal
  and NotifyFilenameChanged skip removal when this session was locked
  out or another session holds the lock, and PruneSwapDir skips held
  journals. A second session used to delete the first session's live
  journal on save or close.
- The writer thread's periodic fsync now runs under mtx_. It fsynced fds
  that the main thread could close concurrently (or that had been
  reused); an in-flight fsync also kept a just-closed journal's flock
  alive, so the owner's own cleanup saw its journal as held.
- Compaction locks the compacted file before renaming it into place and
  keeps using it, so the journal is never unlocked in between; a failed
  lock on the truncating reopen marks the session locked out.
- Unnamed-buffer journals include the pid, so sessions no longer share
  (and delete) each other's unnamed-N.swp.
- RowsView keeps only the previous generation of cached rows: keeping
  all of them made C-u 2000 M-d on a 1 MiB line use ~2 GB.
- Renderers run regex match highlighting through ForEachRegexMatch,
  which uses the large-stack worker for lines over 512 bytes; some
  patterns overflowed the main stack well under the previous 10,000
  byte limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
2026-09-24 09:16:04 -07:00

70 lines
1.6 KiB
C++

#include "RegexGuard.h"
#include <exception>
#include <pthread.h>
namespace kte {
namespace {
// 1 GiB supports matches spanning several million characters.
constexpr std::size_t kLargeStackBytes = std::size_t{1} << 30;
struct Job {
const std::function<void()> *fn;
std::exception_ptr error;
};
void *
run_job(void *arg)
{
auto *job = static_cast<Job *>(arg);
try {
(*job->fn)();
} catch (...) {
job->error = std::current_exception();
}
return nullptr;
}
} // namespace
void
RunWithLargeStack(const std::function<void()> &fn)
{
Job job{&fn, nullptr};
pthread_attr_t attr;
bool started = false;
pthread_t tid{};
if (pthread_attr_init(&attr) == 0) {
if (pthread_attr_setstacksize(&attr, kLargeStackBytes) == 0)
started = pthread_create(&tid, &attr, run_job, &job) == 0;
pthread_attr_destroy(&attr);
}
if (!started) {
fn();
return;
}
pthread_join(tid, nullptr);
if (job.error)
std::rethrow_exception(job.error);
}
void
ForEachRegexMatch(const std::string &line, const std::regex &rx,
const std::function<void(std::size_t, std::size_t)> &on_match)
{
auto scan = [&] {
for (auto it = std::sregex_iterator(line.begin(), line.end(), rx); it != std::sregex_iterator(); ++it)
on_match(static_cast<std::size_t>(it->position()), static_cast<std::size_t>(it->length()));
};
// Stack use grows with match length; a few hundred bytes stays far below
// any thread's stack for every pattern we measured.
constexpr std::size_t kInlineLimit = 512;
if (line.size() <= kInlineLimit)
scan();
else
RunWithLargeStack(scan);
}
} // namespace kte