Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3327d9

Browse files
omigafumarcofu
andauthored
Fix bug: the pipeline mode socket return an unexpected result after reconnecting (#2358)
* fix bug: the pipeline mode socket return an unexpected result after reconnecting * fix typos: pipeline is right --------- Co-authored-by: marcofu <[email protected]>
1 parent 35a7cc0 commit a3327d9

6 files changed

Lines changed: 21 additions & 18 deletions

File tree

cluster_library.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ PHP_REDIS_API int cluster_map_keyspace(redisCluster *c) {
10971097
memset(c->master, 0, sizeof(redisClusterNode*)*REDIS_CLUSTER_SLOTS);
10981098
}
10991099
}
1100-
redis_sock_disconnect(seed, 0);
1100+
redis_sock_disconnect(seed, 0, 1);
11011101
if (mapped) break;
11021102
} ZEND_HASH_FOREACH_END();
11031103

@@ -1218,13 +1218,13 @@ PHP_REDIS_API void cluster_disconnect(redisCluster *c, int force) {
12181218
if (node == NULL) continue;
12191219

12201220
/* Disconnect from the master */
1221-
redis_sock_disconnect(node->sock, force);
1221+
redis_sock_disconnect(node->sock, force, 1);
12221222

12231223
/* We also want to disconnect any slave connections so they will be pooled
12241224
* in the event we are using persistent connections and connection pooling. */
12251225
if (node->slaves) {
12261226
ZEND_HASH_FOREACH_PTR(node->slaves, slave) {
1227-
redis_sock_disconnect(slave->sock, force);
1227+
redis_sock_disconnect(slave->sock, force, 1);
12281228
} ZEND_HASH_FOREACH_END();
12291229
}
12301230
} ZEND_HASH_FOREACH_END();
@@ -1603,7 +1603,7 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
16031603
return -1;
16041604
} else if (timedout || resp == -1) {
16051605
// Make sure the socket is reconnected, it such that it is in a clean state
1606-
redis_sock_disconnect(c->cmd_sock, 1);
1606+
redis_sock_disconnect(c->cmd_sock, 1, 1);
16071607

16081608
if (timedout) {
16091609
CLUSTER_THROW_EXCEPTION("Timed out attempting to find data in the correct node!", 0);

library.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ redis_check_eof(RedisSock *redis_sock, zend_bool no_retry, zend_bool no_throw)
359359
for (retry_index = 0; !no_retry && retry_index < redis_sock->max_retries; ++retry_index) {
360360
/* close existing stream before reconnecting */
361361
if (redis_sock->stream) {
362-
redis_sock_disconnect(redis_sock, 1);
362+
/* reconnect no need to reset mode, it will cause pipeline mode socket exception */
363+
redis_sock_disconnect(redis_sock, 1, 0);
363364
}
364365
/* Sleep based on our backoff algorithm */
365366
zend_ulong delay = redis_backoff_compute(&redis_sock->backoff, retry_index);
@@ -390,7 +391,7 @@ redis_check_eof(RedisSock *redis_sock, zend_bool no_retry, zend_bool no_throw)
390391
}
391392
}
392393
/* close stream and mark socket as failed */
393-
redis_sock_disconnect(redis_sock, 1);
394+
redis_sock_disconnect(redis_sock, 1, 1);
394395
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
395396
if (!no_throw) {
396397
REDIS_THROW_EXCEPTION( errmsg, 0);
@@ -3058,7 +3059,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
30583059
ConnectionPool *p = NULL;
30593060

30603061
if (redis_sock->stream != NULL) {
3061-
redis_sock_disconnect(redis_sock, 0);
3062+
redis_sock_disconnect(redis_sock, 0, 1);
30623063
}
30633064

30643065
address = ZSTR_VAL(redis_sock->host);
@@ -3206,7 +3207,7 @@ redis_sock_server_open(RedisSock *redis_sock)
32063207
* redis_sock_disconnect
32073208
*/
32083209
PHP_REDIS_API int
3209-
redis_sock_disconnect(RedisSock *redis_sock, int force)
3210+
redis_sock_disconnect(RedisSock *redis_sock, int force, int is_reset_mode)
32103211
{
32113212
if (redis_sock == NULL) {
32123213
return FAILURE;
@@ -3228,7 +3229,9 @@ redis_sock_disconnect(RedisSock *redis_sock, int force)
32283229
}
32293230
redis_sock->stream = NULL;
32303231
}
3231-
redis_sock->mode = ATOMIC;
3232+
if (is_reset_mode) {
3233+
redis_sock->mode = ATOMIC;
3234+
}
32323235
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
32333236
redis_sock->watching = 0;
32343237

@@ -4107,7 +4110,7 @@ redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size, size_t *line_siz
41074110
snprintf(buf, buf_size, "read error on connection to %s:%d", ZSTR_VAL(redis_sock->host), redis_sock->port);
41084111
}
41094112
// Close our socket
4110-
redis_sock_disconnect(redis_sock, 1);
4113+
redis_sock_disconnect(redis_sock, 1, 1);
41114114

41124115
// Throw a read error exception
41134116
REDIS_THROW_EXCEPTION(buf, 0);

library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ PHP_REDIS_API char *redis_sock_auth_cmd(RedisSock *redis_sock, int *cmdlen);
8383
PHP_REDIS_API void redis_sock_set_auth(RedisSock *redis_sock, zend_string *user, zend_string *pass);
8484
PHP_REDIS_API void redis_sock_set_auth_zval(RedisSock *redis_sock, zval *zv);
8585
PHP_REDIS_API void redis_sock_free_auth(RedisSock *redis_sock);
86-
PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock, int force);
86+
PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock, int force, int is_reset_mode);
8787
PHP_REDIS_API zval *redis_sock_read_multibulk_reply_zval(RedisSock *redis_sock, zval *z_tab);
8888
PHP_REDIS_API int redis_sock_read_single_line(RedisSock *redis_sock, char *buffer,
8989
size_t buflen, size_t *linelen, int set_err);

redis.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ free_redis_object(zend_object *object)
192192

193193
zend_object_std_dtor(&redis->std);
194194
if (redis->sock) {
195-
redis_sock_disconnect(redis->sock, 0);
195+
redis_sock_disconnect(redis->sock, 0, 1);
196196
redis_free_socket(redis->sock);
197197
}
198198
}
@@ -573,7 +573,7 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
573573

574574
/* if there is a redis sock already we have to remove it */
575575
if (redis->sock) {
576-
redis_sock_disconnect(redis->sock, 0);
576+
redis_sock_disconnect(redis->sock, 0, 1);
577577
redis_free_socket(redis->sock);
578578
}
579579

@@ -632,7 +632,7 @@ PHP_METHOD(Redis, close)
632632
{
633633
RedisSock *redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU);
634634

635-
if (redis_sock_disconnect(redis_sock, 1) == SUCCESS) {
635+
if (redis_sock_disconnect(redis_sock, 1, 1) == SUCCESS) {
636636
RETURN_TRUE;
637637
}
638638
RETURN_FALSE;

redis_session.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ redis_pool_free(redis_pool *pool) {
111111
rpm = pool->head;
112112
while (rpm) {
113113
next = rpm->next;
114-
redis_sock_disconnect(rpm->redis_sock, 0);
114+
redis_sock_disconnect(rpm->redis_sock, 0, 1);
115115
redis_free_socket(rpm->redis_sock);
116116
efree(rpm);
117117
rpm = next;
@@ -1103,7 +1103,7 @@ PS_UPDATE_TIMESTAMP_FUNC(rediscluster) {
11031103
/* Attempt to send EXPIRE command */
11041104
c->readonly = 0;
11051105
if (cluster_send_command(c,slot,cmd,cmdlen) < 0 || c->err) {
1106-
php_error_docref(NULL, E_NOTICE, "Redis unable to update session expiry");
1106+
php_error_docref(NULL, E_NOTICE, "Redis unable to update session expiry");
11071107
efree(cmd);
11081108
return FAILURE;
11091109
}
@@ -1146,7 +1146,7 @@ PS_READ_FUNC(rediscluster) {
11461146
cmdlen = redis_spprintf(NULL, NULL, &cmd, "GET", "s", skey, skeylen);
11471147
c->readonly = 1;
11481148
}
1149-
1149+
11501150
efree(skey);
11511151

11521152
/* Attempt to kick off our command */

sentinel_library.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ free_redis_sentinel_object(zend_object *object)
88
redis_sentinel_object *obj = PHPREDIS_GET_OBJECT(redis_sentinel_object, object);
99

1010
if (obj->sock) {
11-
redis_sock_disconnect(obj->sock, 0);
11+
redis_sock_disconnect(obj->sock, 0, 1);
1212
redis_free_socket(obj->sock);
1313
}
1414
zend_object_std_dtor(&obj->std);

0 commit comments

Comments
 (0)