Skip to content

Commit

Permalink
Reduce locking of linenoise state (#16401)
Browse files Browse the repository at this point in the history
#16388 introduced locking for
linenoise state, which turned out to be too greedy and would cause a
lock to be taken while calling a blocking `read`. This would result in
any writes to stall until something was read on the input.
  • Loading branch information
janisozaur committed Jan 8, 2022
1 parent 4e17bb5 commit 47f126f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/thirdparty/linenoise.hpp
Expand Up @@ -2132,21 +2132,28 @@ inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, con
* initially is just an empty string. */
AddHistory("");

// Write out the prompt
if (write(l.ofd,prompt, static_cast<int>(l.prompt.length())) == -1) return -1;
while(1) {
int c;
char cbuf[4];
int nread;
char seq[3];

[[maybe_unused]] auto ifd = l.ifd;

// Release the lock such that others can still write while we await input
lnstate_mutex.unlock();
#ifdef _WIN32
nread = win32read(&c);
if (nread == 1) {
cbuf[0] = c;
}
#else
nread = unicodeReadUTF8Char(l.ifd,cbuf,&c);
nread = unicodeReadUTF8Char(ifd,cbuf,&c);
#endif
// Take back ownership of the lock, since we are going to modify the state now
lnstate_mutex.lock();
if (nread <= 0) return (int)l.len;

/* Only autocomplete when the callback is set. It returns < 0 when
Expand Down

0 comments on commit 47f126f

Please sign in to comment.