Commit Graph

88 Commits

Author SHA1 Message Date
Michael Grunder
869f3d0ef1
Make redisEnableKeepAlive a no-op on AF_UNIX connections. (#1215)
Fixes #1185
2023-08-18 21:07:25 -07:00
michael-grunder
af95517612 Retry poll(2) if we are intterupted.
This commit adds logic to retry our poll call when waiting for the
connection to complete, in the event that we are interrupted by a
signal.

Additionally we do some simple bookkeeping to keep track of the overall
timeout specified by the user.

Fixes #1206
2023-07-25 10:24:06 -07:00
Michael Grunder
ded32c7d1a
Add a test for the TCP_USER_TIMEOUT option. (#1192)
* Add a test for the TCP_USER_TIMEOUT option.
* Explicitly set errno to ENOTSUP on unsupported OS's
2023-06-01 09:09:11 -07:00
Yossi Gottlieb
6de326e872 Fix a typo in b6a052f. 2023-05-30 14:05:28 -07:00
Viktor Söderqvist
b6a052fe09
Helper for setting TCP_USER_TIMEOUT socket option (#1188)
* Implement redisSetTcpUserTimeout to set socket option TCP_USER_TIMEOUT

* Documentation for redisSetTcpUserTimeout and some more undocumented functions

Documentation for redisReconnect() and the setters of socket options:

* redisKeepAlive()
* redisEnableKeepAliveWithInterval()
* redisSetTcpUserTimeout()
2023-05-29 13:25:34 -07:00
Bjorn Svensson
3b15a04b59
Fixup of PR734: Coverage of hiredis.c (#1124)
Improve coverage (#734)

* Remove duplicate tests

- double covered by:
  "Can parse RESP3 doubles"
- bool covered via:
  "Can parse RESP3 bool"

* Make (connect) timeout in test config general

* Set error string in Unix connect with invalid timeout

Restructure testcase since redisConnectWithTimeout() and
redisConnectUnixWithTimeout() now behaves similar.

* Use quiet flag in lcov/genhtml instead of piping to /dev/null

* Fixup of redisCommandArgv test case

* Update test case to match what it covers

Use new test case info text since the previous one seemed copy&pasted.
The sought coverage was the handling of the parent-chaining
for a double object, which the test case now focuses on.

Co-authored-by: Ariel <ashtul@gmail.com>
2022-10-14 08:11:40 -07:00
Michael Grunder
79ae5ffc69
Fix protocol error (#1106)
Fix ProtocolError

This commit attempts to fix hiredis such that a recoverable write error
will be retried rather than throwing a hard error.

Since our read/write functions are now behind function pointers, we
specify semantically that a return value of < 0 is a hard error, 0 a
recoverable error, and > 0 a success.

Our default `redisNetRead` function was already doing something similar
so this also improves code consistency.

Resolves #961

Co-authored-by: Maksim Tuleika <maksim.tuleika@appcast.io>
2022-09-05 11:59:21 -07:00
Michael Grunder
61b5b299f0
Use a windows specific keepalive function. (#1104)
Use a windows specific keepalive function.

While it is possible to toggle `TCP_KEEPALIVE` in windows via
setsockopt, you have to use `WSAIoctl` to set the interval.

Since `WSAIoctl` can actually do all of this in one call (toggle the
option, and set the corresponding interval), just use that in Windows
and avoid the call to `setsockopt` alltogether.

Fixes: #1100
2022-09-03 12:39:57 -07:00
Viktor Söderqvist
10c78c6e17 Add possibility to prefer IPv6, IPv4 or unspecified 2022-09-01 10:42:36 -07:00
Arseniy Simonov
75cb6c1ea6 Do store command timeout in the context for redisSetTimeout (#593) 2022-08-29 16:22:20 -07:00
michael-grunder
f246ee433d Whitespace, style 2022-06-26 14:14:58 -07:00
Kristján Valur Jónsson
94c1985bde Use correct type for getsockopt() 2022-06-26 13:19:40 -07:00
Kristján Valur Jónsson
5e002bc21c Support failed async connects on windows. 2022-06-26 13:19:33 -07:00
valentinogeron
38b5ae543f
add a command_timeout to redisContextOptions (#839)
Add an additional timeout so the user has a convenient way of controlling distinct connect and command timeouts
2020-07-26 12:32:27 -07:00
OmriSteiner
392de5d7f9
fix #785: defer TCP_NODELAY in async tcp connections (#836)
Co-authored-by: Omri Steiner <omri@insoundz.com>
2020-06-22 13:20:30 -07:00
Michael Grunder
6448f735d5
sdsrange overflow fix (#830)
Fix overflow bug in `sdsrange`
2020-06-07 14:38:16 -07:00
Michael Grunder
8e0264cfd6
Allow users to replace allocator and handle OOM everywhere. (#800)
* Adds an indirection to every allocation/deallocation to allow users to 
  plug in ones of their choosing (use custom functions, jemalloc, etc).

* Gracefully handle OOM everywhere in hiredis.  This should make it possible
  for users of the library to have more flexibility in how they handle such situations.

* Changes `redisReaderTask->elements` from an `int` to a `long long` to prevent
  a possible overflow when transferring the task elements into a `redisReply`.

* Adds a configurable `max elements` member to `redisReader` that defaults to
  2^32 - 1.  This can be set to "unlimited" by setting the value to zero.
2020-05-22 09:27:49 -07:00
Michael Grunder
cc9d032971
Win32 tests and timeout fix (#776)
Unit tests in Windows and a Windows timeout fix

This commit gets our unit tests compiling and running on Windows as well as removes a duplicated `timeval` -> `DWORD` conversion logic in sockcompat.c 

There are minor differences in behavior between Linux and Windows to note:

1.  In Windows, opening a non-existent hangs forever in WSAPoll whereas
    it correctly returns with a "Connection refused" error on Linux.
    For that reason, I simply skip this test in Windows.

    It may be related to this known issue:
    https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/

2.  Timeouts are handled slightly differently in Windows and Linux.  
    In Linux, we intentionally set REDIS_ERR_IO for connection
    timeouts whereas in Windows we set REDIS_ERR_TIMEOUT.  It may be
    prudent to fix this discrepancy although there are almost certainly
    users relying on the current behavior.
2020-04-02 22:41:34 -07:00
Michael Grunder
669ac9d0c8
Safe allocation wrappers (#754)
Create allocation wrappers with a configurable OOM handler (defaults to abort()).

See #752, #747
2020-01-28 12:13:05 -08:00
Mark Nunberg
9c7c694cb7
Merge pull request #670 from jman-krafton/master
fix timeout code in windows
2019-08-13 08:50:41 -04:00
Sangmoon Yi
8e61d5737a add recv error code for clarifying timeout 2019-08-12 10:55:08 +09:00
Sangmoon Yi
ab1762cd92 fix timeout code in windows 2019-08-12 10:54:12 +09:00
Quentin Young
83d3c097ef Remove unnecessary null check before free 2019-07-03 21:36:48 +00:00
m
dc6d19b9ec Port network layer to Winsock
With this change, Hiredis builds with MinGW and runs on Windows.
2019-04-01 11:54:37 +02:00
m
1d092a235a Use recv/send instead of read/write
The recv/send calls are more portable than read/write, since unlike the
latter, the former work with Windows sockets.

We also check for EWOULDBLOCK instead of EAGAIN. On most Unices, EAGAIN
and EWOULDBLBOCK are the same thing. However, on Windows they are
different, and send/recv are expected to give EWOULDBLOCK for
non-blocking sockets.
2019-04-01 11:52:24 +02:00
m
e84086cb92 Introduce a redisFD type
The redisFD type should be equal to the system native socket file
desciptor type (for POSIX, this is a plain int).

We also introduce the REDIS_INVALID_FD value, which maps to -1 on POSIX
systems.
2019-04-01 11:52:24 +02:00
m
1788f41f16 Move network I/O calls to net.c
This makes hiredis.c free from system calls related to socket I/O. This
is also makes the treatment of raw socket connections more similar to
the SSL backend.
2019-04-01 11:52:24 +02:00
Mark Nunberg
4b90429640 Remove redundant line after rebase 2019-02-20 09:11:10 -05:00
Mark Nunberg
3a547b8ec0 Unix: set addrlen so async reconnect uses proper size 2019-02-20 09:10:10 -05:00
Code Hz
be6bb39fda
Fix Invalid argument after redisAsyncConnectUnix 2019-01-28 21:54:42 +08:00
Mark Nunberg
088d1469b3 Fix regression when connecting with Unix sockets (#629) 2018-12-05 07:47:27 -05:00
Mark Nunberg
cbe4ae63ae Handle connection errors better in blocking mode as well 2018-09-25 20:21:40 -04:00
Mark Nunberg
5e6bbf8c60 saddr should be addrlen bytes
Not sizeof saddr.
2018-09-25 20:21:40 -04:00
Mark Nunberg
49974c9359 Call connect(2) again for non-blocking connect
This retrieves the actual error which occurred, as getsockopt is not
always reliable in this regard.
2018-09-25 20:21:37 -04:00
Justin Brewer
58e6b87f51 Remove redundant NULL checks
free(NULL) is a valid NOP. Most of the hiredis free functions behave the
same way. redisReaderFree is updated to also be NULL-safe.

There is one redundant NULL check at sds.c:1036, but it's left as is
since sds is imported from upstream.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
546d9415e1 Fix a segfault on *BSD
freeaddrinfo is not required by POSIX to be NULL-safe. OpenBSD will
SIGSEGV. NetBSD will assert. FreeBSD up to 11.1 will SIGSEGV, while in
future versions, it will be a silent NOP [1].

Commit d4b715f0aa ("Fix potential race in 'invalid timeout' tests")
added a code path to _redisContextConnectTcp which calls
freeaddrinfo(NULL), triggering the segfault. Put a NULL check around the
call to freeaddrinfo.

[1] - e916723903

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
d1c1b668c1 Drop __redis_strerror_r
Since _GNU_SOURCE is now guaranteed to be unset, it is no longer
necessary to support the GNU-specific version of strerror_r.

Drop __redis_strerror_r from the header, and call strerror_r directly.
This breaks any external users of this macro, but they shouldn't have
been using it in  the first place.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
49bbaacc79 Strip down fmacros.h
strerror_r and addrinfo require _POSIX_C_SOURCE >= 200112L,  which is
implied by _XOPEN_SOURCE >= 600. With the removal of AF_LOCAL usage,
the only non-standard features being used are the TCP_KEEP* socket
flags. _DARWIN_C_SOURCE is required to expose TCP_KEEPALIVE.

Fall back to using _XOPEN_SOURCE 600 for all platforms, and
additionally define _DARWIN_C_SOURCE for Darwin.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-28 16:35:40 -05:00
Justin Brewer
bbeab80090 Use AF_UNIX
AF_LOCAL is the old, non-standardized name for AF_UNIX. Just use
AF_UNIX, rather than wrestling with platform specifics of AF_LOCAL
definitions.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-28 16:35:38 -05:00
not-a-robot[bot]
97d611e9b5 Merge #533
533: Small fixes r=badboy
2017-07-15 14:50:49 +00:00
amallia
3c32344a41 Small fixes 2017-06-15 20:59:37 +01:00
Frederik Deweerdt
e21c9c6729 Fix leak if setsockopt fails 2017-05-15 09:14:12 -07:00
Jin Qing
25cd884f6b Fix __redisSetErrorFromErrno() can not get error string.
snprintf() may change errno.
2016-12-30 23:13:02 +08:00
WSL
64d1ec8342 fix: should close socket fd when retry connet remote (tcp) 2016-07-07 17:02:06 +08:00
Tom Lee
d4b715f0aa Fix potential race in 'invalid timeout' tests
It's possible for the call to connect() to succeed on the very first
try, in which case the logic for checking for invalid timeout fields is
never executed. When this happens, the tests fail because they expect a
REDIS_ERR_IO but no such failure has occurred.

Tests aside, this is a potential source of irritating and hard-to-find
intermittent bugs.

This patch forces the validation to occur early so that we get
predictable behavior whenever an invalid timeout is specified.
2015-11-18 00:36:29 -08:00
Alex Balashov
d132d676e9 Renamed redisContext struct member 'unix' to 'unix_sock' to avoid encountering defined constant 'unix' in GNU C environment (see commit d8145d79ce).
Not all code using hiredis can compile using '-std=c99', and/or not all users are able to easily make that change to the build process of various open-source projects, so it is more pragmatic to choose a different identifier that does not impose this requirement.
2015-04-30 15:01:31 -04:00
Jan-Erik Rediger
af598dbce5 Change copyright date and add copyright holder 2015-04-16 21:29:41 +02:00
Jan-Erik Rediger
d9e0b0f6ab Implement a reconnect method for the client context
Originally implemented by @abedra as part of #306.

In case a write or read times out, we force an error state, because we
can't guarantuee that the next read will get the right data.
Instead we need to reconnect to have a clean-state connection, which is
now easily possible with this method.
2015-04-16 21:00:30 +02:00
Matt Stancliff
cc20232406 Fix errno error buffers to not clobber errors
The strerror_r API has two flavors depending on system options.

The bad flavor uses a static buffer for returning results, so if
you save the pointer from strerror_r, the string you're referencing
becomes useless if anybody else calls strerror_r again

The good flavor does what you expect: it writes the error to your buffer.

This commit uses strerror_r directly if it's a good version or copies
the static buffer into our private buffer if it's a bad version.

Thanks to gemorin for explaining the problem and drafting a fix.

Fixes #239
2015-01-05 16:53:23 -05:00
Chris Lamb
85c6bfb02a Fix build under kfreebsd
Signed-off-by: Chris Lamb <chris@chris-lamb.co.uk>

[Instead of checking for "not solaris" we feature detect
for availability of what we want, then remove the system
that advertises compatability but doesn't actually provide it
(given our assumptions about what we're guarding).]

Closes #254
2015-01-05 16:39:30 -05:00