ManUtils: Fix possible deadlock on groff I/O

This commit is contained in:
Yorhel 2025-03-02 21:37:54 +01:00
parent 026a9bf2ad
commit e33faef861

View file

@ -5,6 +5,7 @@ use IPC::Open3;
use IO::Poll qw/POLLOUT POLLIN/; use IO::Poll qw/POLLOUT POLLIN/;
use Symbol 'gensym'; use Symbol 'gensym';
use XSLoader; use XSLoader;
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
XSLoader::load('ManUtils'); XSLoader::load('ManUtils');
sub fmt($input) { sub fmt($input) {
@ -27,6 +28,10 @@ sub fmt($input) {
my $pid = open3(my $in, my $out, my $err = gensym, @cmd); my $pid = open3(my $in, my $out, my $err = gensym, @cmd);
# "POLLOUT" does NOT guarantee that a write will not block, we must set
# O_NONBLOCK to tell the kernel that we can handle short writes.
fcntl($in, F_SETFL, fcntl($in, F_GETFL, 0) | O_NONBLOCK);
my $inoff = 0; my $inoff = 0;
my $output = ''; my $output = '';
my $poll = IO::Poll->new; my $poll = IO::Poll->new;