Catch wrong credentials for pSQL #6422

Closed
opened 2024-08-06 20:30:57 +02:00 by Kowlin · 9 comments
Kowlin commented 2024-08-06 20:30:57 +02:00 (Migrated from github.com)

The fact that no one caught this after all these years... is kinda funny!

(venv) C:\Users\oriko\coding\red>redbot ori299
The main bot task didn't handle an exception and has crashed
Traceback (most recent call last):
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 470, in red_exception_handler
    red_task.result()
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 315, in run_bot
    await driver_cls.initialize(**data_manager.storage_details())
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\core\_drivers\postgres\postgres.py", line 47, in initialize
    cls._pool = await asyncpg.create_pool(**storage_details)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 403, in _async__init__
    await self._initialize()
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 430, in _initialize
    await first_ch.connect()
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 128, in connect
    self._con = await self._pool._get_new_connection()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 502, in _get_new_connection
    con = await connection.connect(
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connection.py", line 2329, in connect
    return await connect_utils._connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 991, in _connect
    conn = await _connect_addr(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 828, in _connect_addr
    return await __connect_addr(params, True, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 876, in __connect_addr
    await connected
asyncpg.exceptions.InvalidPasswordError: password authentication failed for user "oriko"
Attempting to die as gracefully as possible...
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<shutdown_handler() done, defined at C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py:425> exception=AttributeError("'Red' object has no attribute '_AutoShardedClient__queue'")>
Traceback (most recent call last):
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 441, in shutdown_handler
    await red.close()
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\core\bot.py", line 2178, in close
    await super().close()
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\ext\commands\bot.py", line 247, in close
    await super().close()  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\shard.py", line 498, in close
    await self._closing_task
  File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\shard.py", line 495, in _close
    self.__queue.put_nowait(EventItem(EventType.clean_close, None, None))
    ^^^^^^^^^^^^
AttributeError: 'Red' object has no attribute '_AutoShardedClient__queue'
The fact that no one caught this after all these years... is kinda funny! <details> ``` (venv) C:\Users\oriko\coding\red>redbot ori299 The main bot task didn't handle an exception and has crashed Traceback (most recent call last): File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 470, in red_exception_handler red_task.result() File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 315, in run_bot await driver_cls.initialize(**data_manager.storage_details()) File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\core\_drivers\postgres\postgres.py", line 47, in initialize cls._pool = await asyncpg.create_pool(**storage_details) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 403, in _async__init__ await self._initialize() File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 430, in _initialize await first_ch.connect() File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 128, in connect self._con = await self._pool._get_new_connection() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\pool.py", line 502, in _get_new_connection con = await connection.connect( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connection.py", line 2329, in connect return await connect_utils._connect( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 991, in _connect conn = await _connect_addr( ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 828, in _connect_addr return await __connect_addr(params, True, *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\asyncpg\connect_utils.py", line 876, in __connect_addr await connected asyncpg.exceptions.InvalidPasswordError: password authentication failed for user "oriko" Attempting to die as gracefully as possible... Task exception was never retrieved future: <Task finished name='Task-2' coro=<shutdown_handler() done, defined at C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py:425> exception=AttributeError("'Red' object has no attribute '_AutoShardedClient__queue'")> Traceback (most recent call last): File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\__main__.py", line 441, in shutdown_handler await red.close() File "C:\Users\oriko\coding\red\venv\Lib\site-packages\redbot\core\bot.py", line 2178, in close await super().close() File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\ext\commands\bot.py", line 247, in close await super().close() # type: ignore ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\shard.py", line 498, in close await self._closing_task File "C:\Users\oriko\coding\red\venv\Lib\site-packages\discord\shard.py", line 495, in _close self.__queue.put_nowait(EventItem(EventType.clean_close, None, None)) ^^^^^^^^^^^^ AttributeError: 'Red' object has no attribute '_AutoShardedClient__queue' ``` </details>
japandotorg commented 2024-11-05 17:01:28 +01:00 (Migrated from github.com)

Should config fallback to use the JSON driver with a specified message sent to the bot owner(s) if postgres credentials are wrong?

Should config fallback to use the JSON driver with a specified message sent to the bot owner(s) if postgres credentials are wrong?
Flame442 commented 2024-11-05 18:53:56 +01:00 (Migrated from github.com)

I would think not, as that would be an entirely separate set of data. I would say not being able to load its data because the credentials are incorrect should cause the bot to exit with the "configuration error" status code (78).
github.com/Cog-Creators/Red-DiscordBot@30058c0f73/redbot/core/_cli.py (L26)

Though I am curious how we handle the database not running when the bot starts. This can probably be handled in a similar way to that as well.

I would think not, as that would be an entirely separate set of data. I would say not being able to load its data because the credentials are incorrect should cause the bot to exit with the "configuration error" status code (78). https://github.com/Cog-Creators/Red-DiscordBot/blob/30058c0f732ffcf82124bab8781f8b33d1353de4/redbot/core/_cli.py#L26 Though I am curious how we handle the database not running when the bot starts. This can probably be handled in a similar way to that as well.
japandotorg commented 2024-11-05 19:07:37 +01:00 (Migrated from github.com)

Does Red initially tries to connect to postgres when starting the bot, or does it only connect individually to postgres when a cog uses Config?

Does Red initially tries to connect to postgres when starting the bot, or does it only connect individually to postgres when a cog uses Config?
japandotorg commented 2024-11-05 19:10:44 +01:00 (Migrated from github.com)

is there like any "first guaranteed call to postgres" when Red initially starts when using the postgres driver?

is there like any "first guaranteed call to postgres" when Red initially starts when using the postgres driver?
Flame442 commented 2024-11-06 03:06:13 +01:00 (Migrated from github.com)

github.com/Cog-Creators/Red-DiscordBot@30058c0f73/redbot/main.py#L300-L315
github.com/Cog-Creators/Red-DiscordBot@30058c0f73/redbot/core/_drivers/postgres/postgres.py (L42-L47)

Currently the pool is created in run_bot (which is before bot.start()), without any guards for if the pool is not online or the password is incorrect. I still would be inclined to say that the entire driver_cls.initialize call should be wrapped in a try/except that sys.exit(ExitCodes.CONFIGURATION_ERROR) on exception.

https://github.com/Cog-Creators/Red-DiscordBot/blob/30058c0f732ffcf82124bab8781f8b33d1353de4/redbot/__main__.py#L300-L315 https://github.com/Cog-Creators/Red-DiscordBot/blob/30058c0f732ffcf82124bab8781f8b33d1353de4/redbot/core/_drivers/postgres/postgres.py#L42-L47 Currently the pool is created in `run_bot` (which is before `bot.start()`), without any guards for if the pool is not online or the password is incorrect. I still would be inclined to say that the entire `driver_cls.initialize` call should be wrapped in a `try`/`except` that `sys.exit(ExitCodes.CONFIGURATION_ERROR)` on exception.
japandotorg commented 2024-11-08 14:42:56 +01:00 (Migrated from github.com)

Shouldn't this also be handled when lets say the creds changed during runtime?

Shouldn't this also be handled when lets say the creds changed during runtime?
Kowlin commented 2024-11-08 14:51:22 +01:00 (Migrated from github.com)

Shouldn't this also be handled when lets say the creds changed during runtime?

That sounds like a very specific edge case. Credentials don't normally change during runtime. So in my opinion its fine to skip over it.

> Shouldn't this also be handled when lets say the creds changed during runtime? That sounds like a very specific edge case. Credentials don't normally change during runtime. So in my opinion its fine to skip over it.
Flame442 commented 2024-11-08 14:56:15 +01:00 (Migrated from github.com)

The database going offline during runtime is a less rare edge case than the credentials changing, and both would be handled similarly, so I don't think it's unreasonable to consider. That being said, it's less clear where it would be best to catch these errors during runtime, and it's less likely to be an issue than not configuring the connection correctly at the start. I'd say that handling these on bot start is much more important, and should be the main focus for someone working on this issue. Catching similar errors during runtime wouldn't hurt, but I think is lower priority.

The database going offline during runtime is a less rare edge case than the credentials changing, and both would be handled similarly, so I don't think it's unreasonable to consider. That being said, it's less clear where it would be best to catch these errors during runtime, and it's less likely to be an issue than not configuring the connection correctly at the start. I'd say that handling these on bot start is much more important, and should be the main focus for someone working on this issue. Catching similar errors during runtime wouldn't hurt, but I think is lower priority.
Owner

Behoben in 46f0107d5: Falsche Zugangsdaten, eine fehlende Datenbank oder ein nicht erreichbarer Server führen nicht mehr zu einem Traceback. Red loggt den Grund und wo die Verbindungsdaten gespeichert sind und beendet sich mit Exit-Code 78 (Konfigurationsfehler).


Generated by Claude Code

Behoben in 46f0107d5: Falsche Zugangsdaten, eine fehlende Datenbank oder ein nicht erreichbarer Server führen nicht mehr zu einem Traceback. Red loggt den Grund und wo die Verbindungsdaten gespeichert sind und beendet sich mit Exit-Code 78 (Konfigurationsfehler). --- _Generated by [Claude Code](https://claude.ai/code)_
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot#6422
No description provided.