Skip to content

Commit 00d33f4

Browse files
authored
Address issue that returned poll immediately (#231)
fix(poller): address issue that returned `poll` immediately Fix the issue because of which `poll` always returned immediately without waiting for the actual event (TLS handshake flow uses proper watcher now).
1 parent e2641a1 commit 00d33f4

11 files changed

+57
-41
lines changed

.pubnub.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: c-core
22
schema: 1
3-
version: "5.3.1"
3+
version: "5.3.2"
44
scm: github.com/pubnub/c-core
55
changelog:
6+
- date: 2025-09-23
7+
version: v5.3.2
8+
changes:
9+
- type: bug
10+
text: "Fix the issue because of which `poll` always returned immediately without waiting for the actual event (TLS handshake flow uses proper watcher now)."
611
- date: 2025-09-17
712
version: v5.3.1
813
changes:
@@ -1031,7 +1036,7 @@ sdks:
10311036
distribution-type: source code
10321037
distribution-repository: GitHub release
10331038
package-name: C-Core
1034-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1039+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
10351040
requires:
10361041
-
10371042
name: "miniz"
@@ -1097,7 +1102,7 @@ sdks:
10971102
distribution-type: source code
10981103
distribution-repository: GitHub release
10991104
package-name: C-Core
1100-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1105+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
11011106
requires:
11021107
-
11031108
name: "miniz"
@@ -1163,7 +1168,7 @@ sdks:
11631168
distribution-type: source code
11641169
distribution-repository: GitHub release
11651170
package-name: C-Core
1166-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1171+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
11671172
requires:
11681173
-
11691174
name: "miniz"
@@ -1225,7 +1230,7 @@ sdks:
12251230
distribution-type: source code
12261231
distribution-repository: GitHub release
12271232
package-name: C-Core
1228-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1233+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
12291234
requires:
12301235
-
12311236
name: "miniz"
@@ -1286,7 +1291,7 @@ sdks:
12861291
distribution-type: source code
12871292
distribution-repository: GitHub release
12881293
package-name: C-Core
1289-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1294+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
12901295
requires:
12911296
-
12921297
name: "miniz"
@@ -1342,7 +1347,7 @@ sdks:
13421347
distribution-type: source code
13431348
distribution-repository: GitHub release
13441349
package-name: C-Core
1345-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1350+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
13461351
requires:
13471352
-
13481353
name: "miniz"
@@ -1395,7 +1400,7 @@ sdks:
13951400
distribution-type: source code
13961401
distribution-repository: GitHub release
13971402
package-name: C-Core
1398-
location: https://github.com/pubnub/c-core/releases/tag/v5.3.1
1403+
location: https://github.com/pubnub/c-core/releases/tag/v5.3.2
13991404
requires:
14001405
-
14011406
name: "miniz"

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v5.3.2
2+
September 23 2025
3+
4+
#### Fixed
5+
- Fix the issue because of which `poll` always returned immediately without waiting for the actual event (TLS handshake flow uses proper watcher now).
6+
17
## v5.3.1
28
September 17 2025
39

core/pbpal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ enum pbpal_resolv_n_connect_result {
3737
enum pbpal_tls_result {
3838
pbtlsEstablished,
3939
pbtlsStarted,
40+
pbtlsStartedWaitRead,
41+
pbtlsStartedWaitWrite,
4042
pbtlsInProgress,
4143
pbtlsResourceFailure,
4244
pbtlsFailed
4345
};
4446

4547
/* Handles socket condition on given platform */
46-
enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char const* file, int line);
48+
enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char const* file, int line, bool *needRead, bool* needWrite);
4749

4850
/** Handles start of a TCP (HTTP) connection. It first handles DNS
4951
resolving for the context @p pb. If DNS is already resolved, it

core/pubnub_netcore.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,11 @@ int pbnc_fsm(struct pubnub_* pb)
723723
case pbtlsEstablished:
724724
break;
725725
case pbtlsStarted:
726+
case pbtlsStartedWaitRead:
727+
case pbtlsStartedWaitWrite:
726728
pb->state = PBS_WAIT_TLS_CONNECT;
729+
if (pbtlsStartedWaitWrite == res) pbntf_watch_out_events(pb);
730+
if (pbtlsStartedWaitRead == res) pbntf_watch_in_events(pb);
727731
return 0;
728732
case pbtlsFailed:
729733
if (pb->options.fallbackSSL) {
@@ -770,6 +774,8 @@ int pbnc_fsm(struct pubnub_* pb)
770774
pb->state = PBS_TX_GET;
771775
goto next_state;
772776
case pbtlsStarted:
777+
case pbtlsStartedWaitRead:
778+
case pbtlsStartedWaitWrite:
773779
break;
774780
case pbtlsFailed:
775781
if (pb->options.fallbackSSL) {

core/pubnub_version_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define INC_PUBNUB_VERSION_INTERNAL
44

55

6-
#define PUBNUB_SDK_VERSION "5.3.1"
6+
#define PUBNUB_SDK_VERSION "5.3.2"
77

88

99
#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */

core/samples/pubnub_sync_sample.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ subscribe_thr(void *pn_sub_addr)
140140

141141
int main()
142142
{
143-
#if defined _WIN32
144-
uintptr_t sub_thread;
145-
#else
146-
pthread_t sub_thread;
147-
#endif
148143
time_t t0;
149144
char const* msg;
150145
enum pubnub_res res;
@@ -169,19 +164,6 @@ int main()
169164

170165
pubnub_set_auth(pbp, "danaske");
171166

172-
#if defined _WIN32
173-
sub_thread = _beginthread(subscribe_thr, 0, pbp);
174-
#else
175-
pthread_create(&sub_thread, NULL, subscribe_thr, pbp);
176-
#endif
177-
puts("Lets go sleeping...");
178-
sleep(4);
179-
puts("Done sleeping. Cancelling subscribe long-poll...");
180-
pubnub_cancel(pbp);
181-
sleep(2);
182-
printf("What we have: %s\n", pubnub_res_2_string(pubnub_last_result(pbp)));
183-
return 0;
184-
185167
puts("Publishing...");
186168
time(&t0);
187169
res = pubnub_publish(pbp, chan, "\"Hello world from sync!\"");

lib/sockets/pbpal_resolv_and_connect_sockets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,12 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t* pb)
420420
#endif
421421
#ifdef PUBNUB_CALLBACK_API
422422
sockaddr_inX_t dest = { 0 };
423+
#if PUBNUB_PROXY_API
423424
#if PUBNUB_USE_IPV6
424425
const bool has_ipv6_proxy = 0 != pb->proxy_ipv6_address.ipv6[0]
425426
|| 0 != pb->proxy_ipv6_address.ipv6[1];
426427
#endif /* PUBNUB_USE_IPV6 */
428+
#endif /* PUBNUB_PROXY_API */
427429

428430
prepare_port_and_hostname(pb, &port, &origin);
429431
#if PUBNUB_PROXY_API

mbedtls/pbpal_connect_mbedtls.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ enum pbpal_tls_result pbpal_check_tls(pubnub_t* pb) {
174174
PUBNUB_ASSERT_OPT(PBS_CONNECTED == pb->state);
175175
PUBNUB_LOG_TRACE("pbpal_check_tls(pb=%p)\n", pb);
176176

177+
bool needRead = false, needWrite = false;
177178
result = mbedtls_ssl_handshake(pb->pal.ssl);
178179

179-
if (PNR_OK != (result = pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__))) {
180+
if (PNR_OK != (result = pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__, &needRead, &needWrite))) {
180181
PUBNUB_LOG_TRACE("pbpal_check_tls(pb=%p) result = %d\n", pb, result);
181-
return (result == PNR_IN_PROGRESS) ? pbtlsStarted : pbtlsFailed;
182+
if (result != PNR_IN_PROGRESS) return pbtlsFailed;
183+
return needRead ? pbtlsStartedWaitRead : needWrite ? pbtlsStartedWaitWrite : pbtlsStarted;
182184
}
183185

184186
PUBNUB_LOG_DEBUG("TLS connection established\n");

mbedtls/pbpal_mbedtls.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int pbpal_send_str(pubnub_t* pb, char const* s)
6363
}
6464

6565

66-
enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char const* file, int line)
66+
enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char const* file, int line, bool *needRead, bool* needWrite)
6767
{
6868
char reason[100] = {0};
6969

@@ -83,6 +83,10 @@ enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char con
8383
if (pbms_active(pb->pal.connection_timer)
8484
|| (pbms_elapsed(pb->pal.connection_timer) < pb->transaction_timeout_ms)) {
8585
PUBNUB_LOG_TRACE("pb=%p TLS/SSL_I/O operation should retry\n", pb);
86+
87+
if (needWrite && MBEDTLS_ERR_SSL_WANT_WRITE == result) *needWrite = true;
88+
if (needRead && MBEDTLS_ERR_SSL_WANT_READ == result) *needRead = true;
89+
8690
return PNR_IN_PROGRESS;
8791
}
8892

@@ -132,7 +136,7 @@ int pbpal_send_status(pubnub_t* pb)
132136

133137
PUBNUB_LOG_TRACE("will write...");
134138
if (0 >= (result = mbedtls_ssl_write(pb->pal.ssl, pb->ptr, pb->len))) {
135-
result = pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__);
139+
result = pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__, NULL, NULL);
136140
} else {
137141
PUBNUB_ASSERT_OPT((unsigned)result <= pb->len);
138142
pb->ptr += result;
@@ -189,7 +193,7 @@ enum pubnub_res pbpal_line_read_status(pubnub_t* pb)
189193
int result = mbedtls_ssl_read(pb->pal.ssl, pb->ptr, pb->left);
190194

191195
if (result <= 0) {
192-
return pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__);
196+
return pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__, NULL, NULL);
193197
}
194198

195199
PUBNUB_ASSERT_OPT(result <= pb->left);
@@ -293,7 +297,7 @@ enum pubnub_res pbpal_read_status(pubnub_t* pb)
293297
have_read = mbedtls_ssl_read(pb->pal.ssl, pb->ptr, to_read);
294298

295299
if (have_read <= 0) {
296-
return pbpal_handle_socket_condition(have_read, pb, __FILE__, __LINE__);
300+
return pbpal_handle_socket_condition(have_read, pb, __FILE__, __LINE__, NULL, NULL);
297301
}
298302

299303
PUBNUB_ASSERT_OPT(pb->left >= have_read);

openssl/pbpal_connect_openssl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static int print_to_pubnub_log(const char* s, size_t len, void* p)
4444
/* Starfields Inc (Class 2) Root certificate.
4545
It was used to sign the server certificate of https://pubsub.pubnub.com.
4646
(at the time of writing this, 2015-06-04).
47+
48+
Valid to: June 29, 2034
4749
*/
4850
static char pubnub_cert_Starfield[] =
4951
"-----BEGIN CERTIFICATE-----\n"
@@ -78,6 +80,7 @@ static char pubnub_cert_Starfield[] =
7880
2 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
7981
i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
8082
83+
Valid to: January 28, 2028
8184
*/
8285
static char pubnub_cert_GlobalSign[] =
8386
"-----BEGIN CERTIFICATE-----\n"
@@ -231,10 +234,12 @@ enum pbpal_tls_result pbpal_check_tls(pubnub_t* pb)
231234
ssl = pb->pal.ssl;
232235
PUBNUB_ASSERT(NULL != ssl);
233236

237+
bool needRead = false, needWrite = false;
234238
rslt = SSL_connect(ssl);
235-
rslt = pbpal_handle_socket_condition(rslt, pb, __FILE__, __LINE__);
239+
rslt = pbpal_handle_socket_condition(rslt, pb, __FILE__, __LINE__, &needRead, &needWrite);
236240
if (PNR_OK != rslt) {
237-
return (rslt == PNR_IN_PROGRESS) ? pbtlsStarted : pbtlsFailed;
241+
if (rslt != PNR_IN_PROGRESS) return pbtlsFailed;
242+
return needRead ? pbtlsStartedWaitRead : needWrite ? pbtlsStartedWaitWrite : pbtlsStarted;
238243
}
239244
PUBNUB_LOG_TRACE("pb=%p: SSL connected\n", pb);
240245
socket_set_rcv_timeout(pb->pal.socket, pb->transaction_timeout_ms);

0 commit comments

Comments
 (0)