Fix FIFO double deregistering issue when closing the buffer after EOF
When a fifo was closed, the fifo event handler would close the fd and unregister it from the event handler, however the hook on BufClose did that as well without checking if the fd was still refering to the fifo. Now we use a Buffer flag Fifo to tag the buffer as still linked to a fifo so that the BufClose hook do not close and unregister a second time
This commit is contained in:
parent
8bbfbc8c72
commit
985464bee5
|
@ -26,6 +26,8 @@ void ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
|
||||||
catch (Kakoune::client_removed&)
|
catch (Kakoune::client_removed&)
|
||||||
{
|
{
|
||||||
ClientManager::instance().remove_client_by_context(*context);
|
ClientManager::instance().remove_client_by_context(*context);
|
||||||
|
// do not forget, after this line, the current closure
|
||||||
|
// is dead, do not access captured data.
|
||||||
EventManager::instance().unwatch(fd);
|
EventManager::instance().unwatch(fd);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,15 +228,21 @@ Buffer* open_or_create(const String& filename, Context& context)
|
||||||
|
|
||||||
Buffer* open_fifo(const String& name , const String& filename, Context& context)
|
Buffer* open_fifo(const String& name , const String& filename, Context& context)
|
||||||
{
|
{
|
||||||
int fd = open(filename.c_str(), O_RDONLY);
|
int fd = open(filename.c_str(), O_RDONLY | O_CLOEXEC);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
throw runtime_error("unable to open " + filename);
|
throw runtime_error("unable to open " + filename);
|
||||||
Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo);
|
Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo);
|
||||||
|
|
||||||
buffer->hook_manager().add_hook(
|
buffer->hook_manager().add_hook("BufClose",
|
||||||
"BufClose", [=](const String&, const Context&)
|
[fd, buffer](const String&, const Context&) {
|
||||||
{ EventManager::instance().unwatch(fd); close(fd); }
|
// Check if fifo is still alive, else fd may
|
||||||
);
|
// refer to another file/socket
|
||||||
|
if (buffer->flags() & Buffer::Flags::Fifo)
|
||||||
|
{
|
||||||
|
EventManager::instance().unwatch(fd);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
EventManager::instance().watch(fd, [buffer](int fd) {
|
EventManager::instance().watch(fd, [buffer](int fd) {
|
||||||
char data[4096];
|
char data[4096];
|
||||||
|
@ -248,8 +254,12 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
|
||||||
ClientManager::instance().redraw_clients();
|
ClientManager::instance().redraw_clients();
|
||||||
if (count <= 0)
|
if (count <= 0)
|
||||||
{
|
{
|
||||||
close(fd);
|
assert(buffer->flags() & Buffer::Flags::Fifo);
|
||||||
|
buffer->flags() &= ~Buffer::Flags::Fifo;
|
||||||
|
// after that this closure will be dead, do not access
|
||||||
|
// any captured data.
|
||||||
EventManager::instance().unwatch(fd);
|
EventManager::instance().unwatch(fd);
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user