Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem removing handlers via fileevent #96

Open
joeb1234567 opened this issue Nov 10, 2023 · 0 comments
Open

Problem removing handlers via fileevent #96

joeb1234567 opened this issue Nov 10, 2023 · 0 comments

Comments

@joeb1234567
Copy link

Apologies if this isn't the correct place to be reporting this issue. If that's the case, I'd be grateful if someone would point me in the right direction.

I have recently been blowing the dust off a Perl/Tk application that I wrote some 20 years ago. I'm pretty sure it was working properly back then, but I tried it against the the perl-tk package in Debian Bookworm (version 1:804.036-1+b2) and it doesn't work as expected.

I have narrowed the problem down to the way fileevent behaves. My application uses a socket and always needs a 'readable' event handler and creates a 'writable' event handler as and when it needs it. So it's regularly calling fileevent to both set and unset the 'writable' event handler.

What I'm seeing with the version of Perl/Tk that I've been using is that when the 'writable' event handler is removed, the 'readable' event handler is also removed. I can work around this behaviour by retrieving the 'readable' event handler before removing the 'writable' one and then re-setting the 'readable' handler.

Here's a script that shows the problem for me:

#!/usr/bin/perl
use strict;
use IO::Socket;
use Tk;

my $mw = MainWindow->new;

sub printHandlers {
    my $sock = shift;

    my $ret = $mw->fileevent($sock, 'readable');
    print("readable: ${ret}\n");

    $ret = $mw->fileevent($sock, 'writable');
    print("writable: ${ret}\n");
}

my $sock = IO::Socket::INET->new (PeerAddr => 'google.com', PeerPort => 80, Proto    => 'tcp');
print "No handlers set...\n";
printHandlers($sock);

$mw->fileevent($sock, 'readable' => sub {});
$mw->fileevent($sock, 'writable' => sub {});
print "\nBoth readable and writable handlers set...\n";
printHandlers($sock);

$mw->fileevent($sock, 'readable' => '');
print "\nReadable handler removed...\n";
printHandlers($sock);

I see the following output:

No handlers set...
readable: 
writable: 

Both readable and writable handlers set...
readable: Tk::Callback=ARRAY(0x556871a43960)
writable: Tk::Callback=ARRAY(0x5568718188a8)

Readable handler removed...
readable: 
writable: 

I would expect the last line of the above output to show a 'writable' handler because only the 'readable' handler was removed.

Interestingly, the following Tcl/Tk script doesn't show this behaviour:

#!/usr/bin/wish

proc readHandler sock {
}

proc writeHandler sock {
}

proc printHandlers sock {
    set ret [fileevent $sock readable]
    puts "readable: ${ret}"
    set ret [fileevent $sock writable]
    puts "writable: ${ret}"
}

set sock [socket "google.com" 80]
puts "No handlers set..."
printHandlers $sock

fileevent $sock readable [list readHandler $sock]
fileevent $sock writable [list writeHandler $sock]
puts ""
puts "Both readable and writable handlers set..."
printHandlers $sock

fileevent $sock readable ""
puts ""
puts "Readble handler removed..."
printHandlers $sock

exit

I see the following output:

No handlers set...
readable: 
writable: 

Both readable and writable handlers set...
readable: readHandler sock55ab4d96fc90
writable: writeHandler sock55ab4d96fc90

Readble handler removed...
readable: 
writable: writeHandler sock55ab4d96fc90

This seems correct to me. Perhaps I'm using the Perl/Tk interface incorrectly. If that's the case I'd be interested to know what I'm doing wrong. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant