I'm using the X11::Protocol package to grab an image from the display, which I then shrink into a thumbnail using ImageMagick. The problem is that in certain cases, usually involving the X11 session shutting down while my script is running, my script seems to go into an infinite loop, using 100% CPU.

I believe I have traced this down to a bug in the X11::Protocol::Socket module, but before I report the bug, I want to run it by you experts.

In X11/Protocol/Connection/Socket.pm, I found this code:

sub get { my($self) = shift; my($len) = @_; my($x, $n, $o) = ("", 0, 0); my($sock) = $$self; until ($o == $len) { $n = $sock->sysread($x, $len - $o, $o); croak $! unless defined $n; $o += $n; } return $x; }

Clearly the purpose of this routine is to read a complete X11 response, which includes a header with the size of the rest of the response. In my case I'm calling the X11 function GetImage on the entire display, so the response is rather large. (It is not compressed.)

I believe the issue is that when the X11 server (the display) exits, the sysread() that my script calls will not return an error, but will instead return a 0, which means EOF. This will cause the "until" loop to never end, assuming sysread will continue to return 0 every time it is called after returning 0 once.

If this is what is happening, it would seem that fix would be to add this line after the "croak" line:

croak "end of file" unless $n;

Does that sound reasonable?

By the way, this file is on CPAN.

Update: I found that someone else reported this bug 8 years ago!


In reply to Bug in X11::Protocol::Socket? by Steve in Sunnyvale

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.