106 Commits

Author SHA1 Message Date
Björn Svensson
7123b87f6d Handle any pipelined unsubscribe in async
Redis responds to an unsubscribe with one or many replies, depending
on the current subscribe state. When channels/patterns names are
provided in a command each given name will trigger a reply even if
duplicated or not subscribed to.
To know when we can return from the subscribed state we need to do
bookkeeping on pending additional unsubscribe replies, and make sure
we receive them all before switching state.
2022-02-03 09:26:01 +01:00
Björn Svensson
b6fb548fc6 Ignore pubsub replies without a channel/pattern 2022-02-02 23:43:21 +01:00
Bjorn Svensson
be41ed60d7
Avoid incorrect call to the previous reply's callback (#1040)
* No reuse of the previous reply callback

When multiple replies are parsed from a socket in one read
a previously found callback might get reused when the current
reply has no known callback.

This can be triggered by the added testcase which unsubscribe to
subscribed (A,B) and a non-subscribed channel (X).
Without this correction a callback for wrong channel is called.
-  In 'unsubscribe B X A', B's callback is called when handling X.
-  Now this is not done, i.e. there is no callback called for X.

* Re-push monitor callback for each reply

MONITORING used the same callback for all replies while parsing
multiple responses. This handling was changed to avoid calling
the wrong callback in some scenarios.
Now also change monitorings repush to work with this change.

Includes an added async monitoring testcase.
2022-01-27 18:30:58 -08:00
Bjorn Svensson
f2ce5980e6
Allow sending commands after sending an unsubscribe (#1036)
* Add test of async commands after unsubscribe

Verify that commands are handled after unsubscribing from a channel.
A command is sent before the `unsubscribe` response is received,
which currently triggers an assert in async.c:567:

`redisProcessCallbacks: Assertion `(c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING)' failed.`

* Handle async commands after an unsubscribe

When unsubscribing from the last channel we move from the `subscribe`
state to a normal state. These states uses different holders for the
command callback information.
By moving the callback info during the state change the callback order
can be maintained.
2022-01-18 10:09:38 -08:00
Bjorn Svensson
ff860e55db
Correction for command timeout during pubsub (#1038)
* Add test of command timeout during pubsub

A timeout of a non-subscribe command will be ignored during pubsub.
It will be handled as an idle timeout and a response is awaited for.

* Correction for command timeout during pubsub

Disconnect when a sent non-subscribe command triggers a timeout.
2022-01-11 10:19:43 -08:00
sundb
f2be748024
Fix integer overflow when format command larger than 4GB (#1030) 2021-12-22 10:45:52 -08:00
Bjorn Svensson
58aacdac65
Handle array response in parallell with pubsub using RESP3 (#1014)
RESP3 allows sending commands in parallell with pubsub handling
and these commands might get responded with a REDIS_REPLY_ARRAY.
This conflicts with the pubsub response handling for RESP2 and
results in a faulty state when using RESP3.

Add functionality to keep track of PUSH/RESP3 support on the connection
and only expect the message type REDIS_REPLY_PUSH as subscribe messages
when once seen.
2021-12-22 10:44:29 -08:00
Bjorn Svensson
d3384260e7
Support PING while subscribing (RESP2) (#1027)
* Handle PING during pubsub in RESP2

* Rename invalid callback list

Some commands are valid to send during a subscribe in RESP2, and
most in RESP3. Renaming the callback list from `invalid` to `replies`
to detail this fact.

* Fix review comment
2021-12-16 12:38:15 -08:00
Bjorn Svensson
da5a4ff362
Add asynchronous test for pubsub using RESP3 (#1012)
* Include `unsubscribe` as a subscribe reply in RESP3

By providing the (p)unsubscribe message via the subscribe callback,
instead of via the push callback, we get the same behavior in RESP3
as in RESP2.

* Add asynchronous test for pubsub using RESP3

The testcase will subscribe to a channel, and via a second client
a message is published to the channel. After receiving the message
the testcase will unsubscribe and disconnect.

This RESP3 testcase reuses the subscribe callback from the RESP2
testcase to make sure we have a common behavior.
2021-12-01 11:43:23 -08:00
Meir Shpilraien (Spielrein)
f5f31ff9b9
Added REDIS_NO_AUTO_FREE_REPLIES flag (#962)
When set hiredis will not automatically free replies in an async context, and the replies must be freed instead by the user.

Co-authored-by: Michael Grunder <michael.grunder@gmail.com>
2021-07-11 11:26:20 -07:00
michael-grunder
5850a8ecd2 Ensure we curry any connect error to an async context. 2021-06-17 13:37:49 -07:00
Kristján Valur Jónsson
e06ecf7e45 Ignore timeout callback from a successful connect 2021-04-08 09:45:49 +00:00
Michael Grunder
7d99b56356
Merge pull request #917 from Nordix/stack-alloc-dict-iter
Stack allocate dict iterators
2021-01-26 11:20:25 -08:00
Bjorn Svensson
4bba72103c Handle OOM during async command callback registration
Unless the callback is pushed to the list it will trigger an assert
in redisProcessCallbacks() when the response arrives.
This change let the user get an early error instead,
available in the async context directly.
2021-01-26 09:57:19 +01:00
Bjorn Svensson
920128a260 Stack allocate dict iterators
Replacing the get & release functions with an initiation
function. Simplifies the code and will make sure we
run subscription callbacks in OOM scenarios.
2021-01-25 16:19:42 +01:00
shiyuge
773d6ea8a7
Copy error to redisAsyncContext on timeout 2020-09-23 15:46:20 +08:00
Michael Grunder
5003906d63
Define a no op assert if we detect NDEBUG (#861)
Addresses #642
2020-07-31 13:23:28 -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
Michael Grunder
2e7d7cbabd
Resp3 oob push support (#841)
Proper support for RESP3 PUSH messages.

By default, PUSH messages are now intercepted and the reply memory freed.  
This means existing code should work unchanged when connecting to Redis
>= 6.0.0 even if `CLIENT TRACKING` were then enabled.

Additionally, we define two callbacks users can configure if they wish to handle
these messages in a custom way:

void redisPushFn(void *privdata, void *reply);
void redisAsyncPushFn(redisAsyncContext *ac, void *reply);

See #825
2020-07-19 18:54:42 -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
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
83bba659b9
Add logic to handle RESP3 push messages (#819)
Fixes #815
2020-05-21 11:12:18 -07:00
Nick Rivera
0184caac9d
Provides an optional cleanup callback for async data. 2020-04-09 20:23:06 -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
Yossi Gottlieb
8715ba5c82 wip: SSL code reorganization, see #705. 2019-08-29 22:09:37 +03:00
Marcus Geelnard
85fee25653 MinGW fix: Use _MSC_VER instead of _WIN32 where appropriate
Use _MSC_VER (instead of _WIN32) for things that are specific for
Visual Studio.

Also remove #include <winsock2.h> from hiredis.h, as it leaks too
many symbols and defines into the global namespace, which is
undesirable for a public interface header. Anyone who uses the
the affected parts of the hiredis API needs to include the
appropriate headers anyway in order to declare struct timeval
variables.
2019-08-09 11:49:02 +02:00
Odin Hultgren Van Der Horst
a1d4da63b8 Removed whitespace before newline
- Removed whitespace before newline
 - Removed win style newline
2019-07-22 11:06:10 +02:00
jinjiazhang
cdb836d5f8 Fix Compile Error On Windows (Visual Studio) 2019-04-13 10:38:34 +08:00
Mark Nunberg
17b4174089 Retain the same semantics for connection error on connection timeout
This ensures that a disconnect occurs.

This commit also ensures that disconnects will clean the socket even if
the user is in no-auto-free mode
2019-03-11 09:20:21 -04:00
Mark Nunberg
5eb6958870 Allow option for async connections to not automatically free 2019-02-21 11:49:25 -05:00
Mark Nunberg
5f633ac4ec fix potential uninitialized read
If callback was set before scheduleTimer was set (i..e before one of the
attach()) calls.
2019-02-20 09:11:10 -05:00
Mark Nunberg
deba8d956d Allow connectWithOptions for async as well 2019-02-20 09:10:10 -05:00
Mark Nunberg
35a0a1f369 read/write timeouts 2019-02-20 09:10:10 -05:00
Mark Nunberg
4127e4488b Don't add dead code for HIREDIS_NOSSL
We changed this to `HIREDIS_SSL`
2019-02-20 09:10:10 -05:00
Mark Nunberg
08efa46599 SSL for async I/O 2019-02-20 09:10:10 -05:00
Mark Nunberg
cbe4ae63ae Handle connection errors better in blocking mode as well 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
michael-grunder
fbc4971d7d Merge remote-tracking branch 'hyjin/master' 2018-05-09 16:37:00 -07:00
not-a-robot[bot]
74be165c84 Merge #524 #525
524: Don't pass a negative value to __redisAsyncCommand if redisFormatSdsCommandArgv fails r=badboy



525: Fix compilation on FreeBSD 10.3 with default compiler r=badboy

Given FreeBSD 10.3 with default compiler:

> $ cc -v
> FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
> Target: x86_64-unknown-freebsd10.3

Defining _XOPEN_SOURCE to 600 on the fixed line leads to the following errors:

> cc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
> net.c:435:29: error: use of undeclared identifier 'AF_LOCAL'
>     if (redisCreateSocket(c,AF_LOCAL) < 0)
>                             ^
> net.c:460:21: error: use of undeclared identifier 'AF_LOCAL'
>     sa.sun_family = AF_LOCAL;
>                     ^
> 2 errors generated.
> 

AF_LOCAL is defined in sys/socket.h within ifdef __BSD_VISIBLE.

__BSD_VISIBLE could be defined in sys/cdefs.h, but it is never done if _XOPEN_SOURCE is defined.

So on FreeBSD _XOPEN_SOURCE shouldn't be defined.
2017-07-15 14:17:07 +00:00
amallia
b46413647f Assert statement calls a function which has side effects 2017-06-15 20:33:23 +01:00
Frederik Deweerdt
706129a161 Don't pass a negative value to __redisAsyncCommand if redisFormatSdsCommandArgv fails 2017-05-15 09:20:03 -07:00
Hyungjin Kim
d4699989ca Use cached local variable instead using accessor 2017-01-30 20:01:01 +09:00
Hyungjin Kim
be76c56b51 Consider sub by pattern when clear subscribed flag 2017-01-30 20:00:04 +09:00
Hyungjin Kim
073dc84399 Counting pending subscribe. Fix #396 2016-09-30 00:26:32 +09:00
charsyam
e2f6ee239c fixing typos 2015-10-28 02:19:24 +09:00
Jan-Erik Rediger
36a9802c54 Prevent crash on pending replies in async code
Fixes #335.
2015-07-27 23:35:02 +02:00
antirez
2fc39eb4c3 Fix memory leak in async spontaneous reply handling
When an asynchronous hiredis connection subscribes to a Pub/Sub channel
and gets an error, and in other related conditions, the function
redisProcessCallbacks() enters a code path where the link is
disconnected, however the function returns before freeing the allocated
reply object. This causes a memory leak. The memory leak was trivial to
trigger in Redis Sentinel, which uses hiredis, every time we tried to
subscribe to an instance that required a password, in case the Sentinel
was configured either with the wrong password or without password at
all. In this case, the -AUTH error caused the leaking code path to be
executed.
2015-04-28 22:00:48 +02:00
Matt Stancliff
6a00a4643b Fix redisAppendCommand error result
Previously, redisvAppendCommand() would return OOM even with formatting
errors.  Now we use OTHER with an error string telling the user the
error was formatting related, not memory related.

This also fixes a potentially worse bug where were would pass error result
of -1 as an actual length to another function taking an unsigned length,
which would result in crash/overallocation/errors.  Now for that case,
we just return an error immediately and stop processing the command.

Fixes #177
2015-01-05 16:52:08 -05:00
Matt Stancliff
9b83ddc2d9 Fix clang analyzer warning
redisAsyncInitialize() can return NULL, but then we pass
the return value from redisAsyncInitialize() into something
dereferencing the return value, which can cause crashies.
2015-01-05 16:39:30 -05:00
mike
7c4d2557c4 Add support for SO_REUSEADDR
[This introduces some new API functions.]

* Adds new flag to the connection context indicating SO_REUSEADDR
  should be set.
* Adds max number of retries constant for when connect() hits
  EADDRNOTAVAIL.
* Adds new function, redisAsyncConnectBindWithReuse(), letting
  clients enable this functionality.

[Removed trailing whitespace in new header lines.]

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