Skip to content

Commit 37251c9

Browse files
sveljkovveljko
authored andcommitted
Do not await disconnect forever (#29)
There was a bug in PubSubClient::stop() - a surplus call to `input_state()`. Also, it now waits for data shorter, not the usual "full (default) time" . This time is not yet user settable. Since ESP8266 can, in some weird and as-of-yet-unexplained situations, take a _long_ time (hours, even) to change the state of the `WiFiClient` to "not connected" after a `stop()`, we no longer await this disconnection "forever". There is now a timeout (for now, not user-settable) and we give up after a short while. This might lead to some (probably temporary) leaks, but, we haven't run into problems in our testing.
1 parent 0f39931 commit 37251c9

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

PubNubDefs.h

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ class PubSubClient : public PubNub_BASE_CLIENT {
179179
}
180180
/* We are still connected. Read the rest of the stream so that
181181
* we catch the timetoken. */
182-
while (wait_for_data()) {
183-
char ch = read();
184-
this->_state_input(ch, 0, 0);
182+
while (wait_for_data(10)) {
183+
read();
185184
}
186185
json_enabled = false;
187186
}
@@ -600,6 +599,17 @@ inline void PubSubClient::_grab_timetoken(uint8_t* nextbuf, size_t nextsize)
600599
timetoken[new_timetoken_len] = 0;
601600
}
602601

602+
inline bool await_disconnect(PubNub_BASE_CLIENT& client, unsigned long timeout) {
603+
unsigned long t_start = millis();
604+
while (client.connected()) {
605+
if (millis() - t_start > timeout * 1000UL) {
606+
return false;
607+
}
608+
delay(10);
609+
}
610+
return true;
611+
}
612+
603613

604614
inline PubNonSubClient* PubNub::publish(const char* channel,
605615
const char* message,
@@ -669,14 +679,16 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
669679
case PubNub_BH_ERROR:
670680
DBGprintln("publish() BH_ERROR");
671681
client.stop();
672-
while (client.connected())
673-
delay(10);
682+
if (!await_disconnect(client, 10)) {
683+
DBGprintln("publish() BH_ERROR: disconnect timeout");
684+
}
674685
return 0;
675686
case PubNub_BH_TIMEOUT:
676687
DBGprintln("publish() BH_TIMEOUT");
677688
client.stop();
678-
while (client.connected())
679-
delay(10);
689+
if (!await_disconnect(client, 10)) {
690+
DBGprintln("publish() BH_TIMEOUT: disconnect timeout");
691+
}
680692
return 0;
681693
}
682694
}
@@ -726,15 +738,17 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
726738
if (!client.wait_for_data()) {
727739
DBGprintln("No data received!");
728740
client.stop();
729-
while (client.connected())
730-
delay(10);
741+
if (!await_disconnect(client, 10)) {
742+
DBGprintln("subscribe() no data received: disconnect timeout");
743+
}
731744
return 0;
732745
}
733746
if (client.read() != '[') {
734747
DBGprintln("Unexpected body in subscribe response");
735748
client.stop();
736-
while (client.connected())
737-
delay(10);
749+
if (!await_disconnect(client, 10)) {
750+
DBGprintln("subscribe() unexpected body: disconnect timeout");
751+
}
738752
return 0;
739753
}
740754
/* Now return handle to the client for further perusal.
@@ -747,17 +761,21 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
747761
case PubNub_BH_ERROR:
748762
DBGprintln("subscribe() BH_ERROR");
749763
client.stop();
750-
while (client.connected())
751-
delay(10);
764+
if (!await_disconnect(client, 10)) {
765+
DBGprintln("subscribe() BH_ERROR: disconnect timeout");
766+
}
752767
return 0;
753768

754769
case PubNub_BH_TIMEOUT:
755770
DBGprintln("subscribe() BH_TIMEOUT");
756771
client.stop();
757772
DBGprintln("subscribe() BH_TIMEOUT stopped");
758-
while (client.connected())
759-
delay(10);
760-
DBGprintln("subscribe() BH_TIMEOUT disconnected");
773+
if (await_disconnect(client, 10)) {
774+
DBGprintln("subscribe() BH_TIMEOUT: disconnected");
775+
}
776+
else {
777+
DBGprintln("subscribe() BH_TIMEOUT: disconnect timeout");
778+
}
761779
return 0;
762780
}
763781
}
@@ -790,14 +808,16 @@ inline PubNonSubClient* PubNub::history(const char* channel, int limit, int time
790808
case PubNub_BH_ERROR:
791809
DBGprintln("history() BH_ERROR");
792810
client.stop();
793-
while (client.connected())
794-
delay(10);
811+
if (!await_disconnect(client, 10)) {
812+
DBGprintln("history() BH_ERROR: disconnect timeout");
813+
}
795814
return 0;
796815
case PubNub_BH_TIMEOUT:
797816
DBGprintln("history() BH_TIMEOUT");
798817
client.stop();
799-
while (client.connected())
800-
delay(10);
818+
if (!await_disconnect(client, 10)) {
819+
DBGprintln("history() BH_TIMEOUT: disconnect timeout");
820+
}
801821
return 0;
802822
}
803823
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Pubnub
2-
version=3.1.1
2+
version=3.2.0
33
author=Vladimir Veljkovic <[email protected]>
44
maintainer=Vladimir Veljkovic <[email protected]>
55
sentence=Pubnub SDK for Arduino.

0 commit comments

Comments
 (0)