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

Skip to content

Invalid Redis socket DSN, the "dbindex" parameter must be a number. #44215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mathielen opened this issue Nov 23, 2021 · 23 comments
Closed

Invalid Redis socket DSN, the "dbindex" parameter must be a number. #44215

mathielen opened this issue Nov 23, 2021 · 23 comments
Labels
Bug Cache Help wanted Issues and PRs which are looking for volunteers to complete them. Status: Needs Review

Comments

@mathielen
Copy link
Contributor

mathielen commented Nov 23, 2021

Symfony version(s) affected

4.4.34

Description

When the Cache component is configured with a Redis DSN pointing to a socket (eg. redis:///var/run/redis/redis-server.sock), an error is thrown: Invalid Redis DSN: "redis:///var/run/redis/redis-server.sock", the "dbindex" parameter must be a number.

This is probably due to #43359 with fix #44043 and an incorrect preg_match in L151.

How to reproduce

<?php
// config/packages/cache.php

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $cache = $framework->cache();

    $cache->app('cache.adapter.redis')
        ->defaultRedisProvider('redis:///var/run/redis/redis-server.sock');

    $cache->pool('my_pool.cache')->adapters(['cache.app']);
};

Possible Solution

Skip the preg_match check if $params['host'] is not set?

Additional Context

No response

@nicolas-grekas
Copy link
Member

/cc @a1812 could you please have a look?

@stof
Copy link
Member

stof commented Nov 23, 2021

Looks like the code wants to use the URL path both for the unix scoket path and the DB index at the same time

@xabbuh xabbuh added the Cache label Nov 23, 2021
@UBERPHP
Copy link

UBERPHP commented Nov 23, 2021

I have the same issue, my env parameter: REDIS_DSN=redis://superpass@/run/redis/domain.local.sock?persistent=1
I was following https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html
v5.3.11 has this issue as well.

@nicolas-grekas
Copy link
Member

Anyone up for a PR fixing this?

@stof
Copy link
Member

stof commented Nov 23, 2021

I think the fix for the dbindex would be to look for it only if we have isset($params['host']) && isset($params['path']). Also, the !isset($params['dbindex'] looks weird to me, as $params is the output of parse_url

@stof stof added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Nov 23, 2021
@UBERPHP
Copy link

UBERPHP commented Nov 23, 2021

if we are talking about format like this redis://[email protected]?path=/another/socket/domain.sock?dbindex=5&persistent=1&something=else,
vendor/symfony/cache/Traits/RedisTrait.php
should be something like this:
image
and the final result would be:
image

However, I think this might be more complicated, need to test all other parameters and other variations
p.s. as @stof mentioned, some parts looks weird to me as well

@UBERPHP
Copy link

UBERPHP commented Nov 23, 2021

based on https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html
image

and using format 'REDIS_DSN' => 'redis://superpass@/run/redis/domain.local.sock?persistent=1&anything=else/db-index=9',

image
looks like correct (uses 'pconnect' and follows documentation)

@UBERPHP
Copy link

UBERPHP commented Nov 23, 2021

apologies for spamming (the previous example was wrongly interpreted by me), this should follow the documentation:
image
however, it's not covering situation when 'db-index' is not set

@quazardous
Copy link

5.3.11 broken for redis://localhost:6379/ (OK with redis://localhost:6379 -> no ending slash)
was OK in 5.3.10

Invalid Redis DSN: "redis://localhost:6379/", the "dbindex" parameter must be a number.

nicolas-grekas added a commit that referenced this issue Nov 23, 2021
…kas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] fix connecting to local Redis sockets

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44215
| License       | MIT
| Doc PR        | -

Commits
-------

954db3f [Cache] fix connecting to local Redis sockets
@loevgaard
Copy link
Contributor

We get the error in 5.3.12. Hasn't this been merged into the 5.3 branch?

@nicolas-grekas
Copy link
Member

The patch is in 5.3.12 I confirm.
Looks like #44232 might not be enough?
Please send a PR if you're affected and can figure out the proper fix.

@loevgaard
Copy link
Contributor

Hi, @nicolas-grekas

This is the diff between 5.3.10 and 5.3.11 (5.3.10 works):

- if (!isset($params['dbindex']) && isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {	
-                $params['dbindex'] = $m[1];	
-                $params['path'] = substr($params['path'], 0, -\strlen($m[0]));
+ if (!isset($params['dbindex']) && isset($params['path'])) {
+                if (preg_match('#/(\d+)$#', $params['path'], $m)) {
+                    $params['dbindex'] = $m[1];
+                    $params['path'] = substr($params['path'], 0, -\strlen($m[0]));
+                } else {
+                    throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s", the "dbindex" parameter must be a + number.', $dsn));
+                }

What was the reason for this change?

@nicolas-grekas
Copy link
Member

What is linked in this issue: see #43359

@loevgaard
Copy link
Contributor

Okay. Could we reopen this issue?

@thomas2411
Copy link
Contributor

I have the same problem as @loevgaard with 5.3.12 What is weird in one project it works OK, in another one it doesn't work, while on both of them I have symfony/cache 5.3.12
I use REDIS locally with MAMP PRO: CACHE_DSN='redis:///Applications/MAMP/tmp/redis.sock'

@mathielen
Copy link
Contributor Author

mathielen commented Feb 2, 2022

(removed)

The new problem is addressed here: #45277 (comment)

@nicolas-grekas
Copy link
Member

@thomas2411 @mathielen you're affected by #45277, not by this issue.

@loevgaard do you have more insights about your issue about dbindex, or does #45281 also fix you issue?

@loevgaard
Copy link
Contributor

I am not experiencing this problem anymore, however, can't be quite sure because I don't quite remember how I triggered the problem (forgot to jot it down :()

@nicolas-grekas
Copy link
Member

OK, please report back with details when you can.

@loevgaard
Copy link
Contributor

Okay, just got this error again: Invalid Redis DSN: "redis://127.0.0.1:51148/", the "dbindex" parameter must be a number..

It happens in here: https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Cache/Traits/RedisTrait.php#L153.

The $dsn inputted in line 85 (https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/Cache/Traits/RedisTrait.php#L85) has this value: redis://127.0.0.1:51148/. An important thing to notice is that the environment variable isREDIS_URL=redis://127.0.0.1:51148 (notice the missing trailing slash) and just as a test I added this $dsn = trim($dsn, '/'); as the first line in the method and then everything works.

@nicolas-grekas
Copy link
Member

Can you send a PR if you have the fix please?

@loevgaard
Copy link
Contributor

Yes, I will, @nicolas-grekas. Thanks!

@loevgaard
Copy link
Contributor

There you have it, @nicolas-grekas. I tested it on the application that gave the error :)

nicolas-grekas added a commit that referenced this issue Feb 13, 2023
…oevgaard)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Cache] Only validate dbindex parameter when applicable

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44215
| License       | MIT

It does not make sense to validate the path parameter when we _know_ that there can't be a dbindex. The problem with this validation is that it actually triggers when DSN looks something like: `redis://127.0.0.1:51148/` which happened to me in rare cases.

Commits
-------

c5e997b [Cache] Only validate dbindex parameter when applicable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Cache Help wanted Issues and PRs which are looking for volunteers to complete them. Status: Needs Review
Projects
None yet
Development

No branches or pull requests

9 participants