Skip to content

Commit 2c0cc68

Browse files
author
Christian Hergert
committed
stream-tls: mind the timeout units.
During the transition to absolute timeouts to handle clock drift the units could have been 1000x the desired timeout value. stream functions take a relative timeout where as we track our absolute timeout using the monotonic clock, which is in microseconds. This changeset adjusts the name to make it clear which units we are using for the timeout to BIO methods. Fixes #43
1 parent a6374de commit 2c0cc68

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/mongoc/mongoc-stream-tls.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ typedef struct
5656
mongoc_stream_t *base_stream;
5757
BIO *bio;
5858
SSL_CTX *ctx;
59-
int32_t timeout;
60-
bool weak_cert_validation;
59+
int32_t timeout_msec;
60+
bool weak_cert_validation;
6161
} mongoc_stream_tls_t;
6262

6363

@@ -206,7 +206,8 @@ _mongoc_stream_tls_bio_read (BIO *b,
206206
}
207207

208208
errno = 0;
209-
ret = (int)mongoc_stream_read (tls->base_stream, buf, len, 0, tls->timeout);
209+
ret = (int)mongoc_stream_read (tls->base_stream, buf, len, 0,
210+
tls->timeout_msec);
210211
BIO_clear_retry_flags (b);
211212

212213
if ((ret < 0) && MONGOC_ERRNO_IS_AGAIN (errno)) {
@@ -253,7 +254,8 @@ _mongoc_stream_tls_bio_write (BIO *b,
253254
iov.iov_len = len;
254255

255256
errno = 0;
256-
ret = (int)mongoc_stream_writev (tls->base_stream, &iov, 1, tls->timeout);
257+
ret = (int)mongoc_stream_writev (tls->base_stream, &iov, 1,
258+
tls->timeout_msec);
257259
BIO_clear_retry_flags (b);
258260

259261
if ((ret < 0) && MONGOC_ERRNO_IS_AGAIN (errno)) {
@@ -480,7 +482,7 @@ _mongoc_stream_tls_writev (mongoc_stream_t *stream,
480482
BSON_ASSERT (iov);
481483
BSON_ASSERT (iovcnt);
482484

483-
tls->timeout = timeout_msec;
485+
tls->timeout_msec = timeout_msec;
484486

485487
if (timeout_msec >= 0) {
486488
expire = bson_get_monotonic_time () + (timeout_msec * 1000UL);
@@ -511,9 +513,9 @@ _mongoc_stream_tls_writev (mongoc_stream_t *stream,
511513
return -1;
512514
}
513515

514-
tls->timeout = 0;
516+
tls->timeout_msec = 0;
515517
} else {
516-
tls->timeout = expire - now;
518+
tls->timeout_msec = (expire - now) / 1000L;
517519
}
518520
}
519521

@@ -567,7 +569,7 @@ _mongoc_stream_tls_readv (mongoc_stream_t *stream,
567569
BSON_ASSERT (iov);
568570
BSON_ASSERT (iovcnt);
569571

570-
tls->timeout = timeout_msec;
572+
tls->timeout_msec = timeout_msec;
571573

572574
if (timeout_msec >= 0) {
573575
expire = bson_get_monotonic_time () + (timeout_msec * 1000UL);
@@ -598,9 +600,9 @@ _mongoc_stream_tls_readv (mongoc_stream_t *stream,
598600
return -1;
599601
}
600602

601-
tls->timeout = 0;
603+
tls->timeout_msec = 0;
602604
} else {
603-
tls->timeout = expire - now;
605+
tls->timeout_msec = (expire - now) / 1000L;
604606
}
605607
}
606608

@@ -727,7 +729,7 @@ mongoc_stream_tls_do_handshake (mongoc_stream_t *stream,
727729

728730
BSON_ASSERT (tls);
729731

730-
tls->timeout = timeout_msec;
732+
tls->timeout_msec = timeout_msec;
731733

732734
if (BIO_do_handshake (tls->bio) == 1) {
733735
return true;
@@ -836,7 +838,7 @@ mongoc_stream_tls_new (mongoc_stream_t *base_stream,
836838
tls->weak_cert_validation = opt->weak_cert_validation;
837839
tls->bio = bio_ssl;
838840
tls->ctx = ssl_ctx;
839-
tls->timeout = -1;
841+
tls->timeout_msec = -1;
840842
bio_mongoc_shim->ptr = tls;
841843

842844
mongoc_counter_streams_active_inc();

0 commit comments

Comments
 (0)