Skip Menu |

This queue is for tickets about the Mail-IMAPClient CPAN distribution.

Report information
The Basics
Id: 114830
Status: rejected
Priority: 0/
Queue: Mail-IMAPClient

People
Owner: PLOBBES [...] cpan.org
Requestors: gdg [...] zplane.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Question on IsConnected() usage
Date: Mon, 30 May 2016 11:51:08 -0600
To: "Phil Pearl (Lobbes) via RT" <bug-Mail-IMAPClient [...] rt.cpan.org>
From: Glenn Golden <gdg [...] zplane.com>
Hi again Phil, I've been trying to use IsConnected() as a simple means for assessing the operational status of the client-server connection, but it doesn't seem to behave as expected over long suspend/resume cycles. Not sure if my expectation is wrong. For example, I have a loop that does something like this: place server into IDLE mode; while (1) { check for traffic via idle_data() and process; sleep a few seconds; every 5 minutes do { if (IsConnected()) { print "Still connected"; } else { teardown and reconnect; } } } If the machine is suspended for several hours and then resumed, the loop continues to hit the "still connected" clause every 5 minutes, but idle_data() always returns 0 (never undef) forever, even though traffic due to folder events is known to be occurring. Is my expectation wrong on this? In your own code, how do you go about checking for operational "workingness"?
On Mon May 30 13:51:19 2016, gdg@zplane.com wrote: Show quoted text
> Hi again Phil, > > I've been trying to use IsConnected() as a simple means for assessing > the > operational status of the client-server connection, but it doesn't > seem to > behave as expected over long suspend/resume cycles. Not sure if my > expectation > is wrong. > > For example, I have a loop that does something like this: > > place server into IDLE mode; > while (1) > { > check for traffic via idle_data() and process;
It may be worth enabling Debug to see what info/hints you get. Are you checking the return value from $idle_data? - If so, does it return undef or an empty array? - Is $self->LastError set? If so, what does it look like? There's a chance that we're failing to recognize a disconnected state as we're perhaps a bit explicit about what we're looking for: - ECONNRESET or empty data returned while attempting Show quoted text
> sleep a few seconds; > every 5 minutes do > { > if (IsConnected())
IsConnected doesn't actually do anything to (re)check the connection state. It is simply looking at the current internal state of Mail::IMAPClient (the current value of the "State" integer internally) sub IsUnconnected { shift->State == Unconnected } You may want to use noop() to "ping" / check the actual health of your connection or just do another call to idle_data as that probably makes more sense for what you describe here. [snip] Show quoted text
> If the machine is suspended for several hours and then resumed, the > loop continues to hit the "still connected" clause every 5 minutes, > but idle_data() always returns 0 (never undef) forever, even though > traffic due to folder events is known to be occurring.
Hmm, it should return an undef, an array or arrayref depending upon context but not "0". Try enabling 'Debug' and also checking if anything is in $self->LastError. Show quoted text
> Is my expectation wrong on this?
Not necessarily, but I'll need more info to help. Show quoted text
> In your own code, how do you go about checking for operational > "workingness"?
Let's see if Debug / LastError info get us closer to troubleshooting the issue. With a quick glance, I suspect we may want to add a little logging within sub __read_more in the code but I'll see if you have any useful info first.
Subject: Re: [rt.cpan.org #114830] Question on IsConnected() usage
Date: Wed, 1 Jun 2016 08:36:48 -0600
To: "Phil Pearl (Lobbes) via RT" <bug-Mail-IMAPClient [...] rt.cpan.org>
From: Glenn Golden <gdg [...] zplane.com>
Hi Phil. Phil Pearl (Lobbes) via RT <bug-Mail-IMAPClient@rt.cpan.org> [2016-05-31 15:17:26 -0400]: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=114830 > > > On Mon May 30 13:51:19 2016, gdg@zplane.com wrote:
> > > > I've been trying to use IsConnected() as a simple means for assessing > > the > > operational status of the client-server connection, but it doesn't > > seem to > > behave as expected over long suspend/resume cycles. Not sure if my > > expectation > > is wrong. > > > > For example, I have a loop that does something like this: > > > > place server into IDLE mode; > > while (1) > > { > > check for traffic via idle_data() and process;
> > It may be worth enabling Debug to see what info/hints you get. >
See the ticket I just filed. It has an example that demonstrates some (not all) of the connectedness/mode issues. Show quoted text
> > Are you checking the return value from $idle_data? > - If so, does it return undef or an empty array? > - Is $self->LastError set? If so, what does it look like? >
Return values are checked on all calls. In the case described above, idle_data() returns an empty array every time and LastError is the empty string. This case is covered in the example in the ticket just filed. Show quoted text
> > > There's a chance that we're failing to recognize a disconnected state > as we're perhaps a bit explicit about what we're looking for: > - ECONNRESET or empty data returned while attempting > >
> > sleep a few seconds; > > every 5 minutes do > > { > > if (IsConnected())
> > IsConnected doesn't actually do anything to (re)check the connection > state. It is simply looking at the current internal state of > Mail::IMAPClient (the current value of the "State" integer internally) > > sub IsUnconnected { shift->State == Unconnected } > > You may want to use noop() to "ping" / check the actual health of your > connection or just do another call to idle_data as that probably makes > more sense for what you describe here. > > [snip] >
> > If the machine is suspended for several hours and then resumed, the > > loop continues to hit the "still connected" clause every 5 minutes, > > but idle_data() always returns 0 (never undef) forever, even though > > traffic due to folder events is known to be occurring.
> > Hmm, it should return an undef, an array or arrayref depending upon > context but not "0". Try enabling 'Debug' and also checking if anything > is in $self->LastError. >
> > Is my expectation wrong on this?
> > Not necessarily, but I'll need more info to help. >
Show quoted text
> > In your own code, how do you go about checking for operational > > "workingness"?
> > Let's see if Debug / LastError info get us closer to troubleshooting > the issue. With a quick glance, I suspect we may want to add a little > logging within sub __read_more in the code but I'll see if you have any > useful info first.
See that ticket I just filed. The example there is not directly applicable to the stuff we're discussing above, but related, and may be helpful. Thanks for your time on this, Glenn
Closing as 'Rejected' as this seems to not be a bug, but more a question on how to determine if a connection is "alive" or not... Using IsConnected isn't likely going to get you what you want. It just looks at the current value of "State" (and internal-ish variable) in the library and doesn't actively go and try to read/write from the server. The question you have seems to be a common, yet non-trivial, question about dealing with clients in a TCP/IP world. I'm not sure what the right answer is for you, but you might consider using Keepalive => 1 and see if that helps you at all. Outside of that, a good test case that reproduces the problem, might help me to find a better answer for you. The following (and many more like it) may also be of some help: http://stackoverflow.com/questions/12091282/how-to-detect-when-socket-connection-is-lost