R
Railwayβ€’11mo ago
guess_wh0

Issues connecting to Postgres Database after redeploying service

Hi, as part of performing the required database-migration, I've redeployed my service for the first time in ~8 months. Building the project still seemed to work just fine, but suddenly there's errors connecting to the database (even before migrating). The strange thing is that (if I remember correctly) originally I was still able to interact with the (old) database before migrating, but after migrating, the server just crashes. This means the timeline is something like this: 1. For 8 months, the service worked well 2. Redeploying the service lead to database-connection-errors, but it was still possible to write/read the database 3. Migrating the databese results in BOTH database-connection-errors as well as crashes and the server becoming inoperable What could be causing these issues? I'm thinking that maybe port/environment-variables changed after the 8 months without my knowledge? If I can provide any logs, I'd be happy to do so. πŸ™‚
Solution:
Actually, it turns out that the crash was caused for different reasons at different places. 1. Rocket seems to decide how many connections to create. More specifically, rocket_sync_db_pools. That package "defaults to the configured number of workers * 4", which seems to be >100 on railway. I assume "workers" means "CPUs", but feel free to correct me if you know more than I do πŸ™‚ Thus, on Railway, there were simply too many connections. 2. Locally, when trying to connect to the Railway-DB, my code only tries to create 32 connections (maybe because of 8 CPUs?). - Thus, I was always able to connect to my local DB - However, connecting to the Railway-DB failed because creating 32 connections within the default timeout of 5s wasn't possible....
Jump to solution
86 Replies
Percy
Percyβ€’11mo ago
Project ID: N/A
Brody
Brodyβ€’11mo ago
do you use reference variables?
guess_wh0
guess_wh0OPβ€’11mo ago
If you mean those, then yes.
No description
Brody
Brodyβ€’11mo ago
and then is DATABASE_URL also a reference variable?
guess_wh0
guess_wh0OPβ€’11mo ago
I'd like to provide a Project ID, but I'm not sure where to find it. The closest thing I can find is the Service ID.
guess_wh0
guess_wh0OPβ€’11mo ago
Yep, and as far as I can tell it's referencing the new database. (That may have been my doing)
No description
Brody
Brodyβ€’11mo ago
looks good to me okay all that's in order, can you share the error message?
guess_wh0
guess_wh0OPβ€’11mo ago
Sure! The original error (after redeploying the server, but before migrating the database):
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Part 2:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
Error: Rocket failed to launch due to failing fairings:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
>> 'voteon_date_db' Database Pool
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
Warning: Received SIGTERM. Requesting shutdown.
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
Error: Rocket failed to launch due to failing fairings:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
>> 'voteon_date_db' Database Pool
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
connection to server at "containers-us-west-22.railway.app" (34.127.45.197), port 6452 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
>> Parameter guard `path: PathWithFileExtension` is forwarding: "No file extension.".
Warning: Received SIGTERM. Requesting shutdown.
Brody
Brodyβ€’11mo ago
still trying to connect to the old database, have you hardcoded the database credentials into your code somewhere?
guess_wh0
guess_wh0OPβ€’11mo ago
The current error, after migrating the database:
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
Error: Rocket failed to launch due to failing fairings:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
Error: Rocket failed to launch due to failing fairings:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Brody
Brodyβ€’11mo ago
okay now that's the new database
guess_wh0
guess_wh0OPβ€’11mo ago
>> 'voteon_date_db' Database Pool
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
>> 'voteon_date_db' Database Pool
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
Exactly
Brody
Brodyβ€’11mo ago
is this the only app you are connecting to the database from?
guess_wh0
guess_wh0OPβ€’11mo ago
Yes. There's only one app in total (+ the old DB and the new DB)
Brody
Brodyβ€’11mo ago
how many pooled connections is your app trying to open?
guess_wh0
guess_wh0OPβ€’11mo ago
I'm not sure what that is. How can I check it?
Brody
Brodyβ€’11mo ago
that would be an in code thing
guess_wh0
guess_wh0OPβ€’11mo ago
Intuitively I'd say one πŸ˜…
Brody
Brodyβ€’11mo ago
not much of a pool then πŸ˜†
guess_wh0
guess_wh0OPβ€’11mo ago
I'll see if I find something to the contrary
Brody
Brodyβ€’11mo ago
either way, remove any deployments on your apps service
guess_wh0
guess_wh0OPβ€’11mo ago
This remove button?
No description
Brody
Brodyβ€’11mo ago
yep and then remove the active deployment on your postgres service
guess_wh0
guess_wh0OPβ€’11mo ago
Both new and old? (legacy and migrated)
Brody
Brodyβ€’11mo ago
legacy databases don't have deployments, so just the v2 database
guess_wh0
guess_wh0OPβ€’11mo ago
Done πŸ‘
Brody
Brodyβ€’11mo ago
then redeploy the v2 database
guess_wh0
guess_wh0OPβ€’11mo ago
In any order?
Brody
Brodyβ€’11mo ago
wdym order?
guess_wh0
guess_wh0OPβ€’11mo ago
Nevermind, I didn't see the edit
Brody
Brodyβ€’11mo ago
do you have some desktop software like dbgate where you could test a connection to postgres?
guess_wh0
guess_wh0OPβ€’11mo ago
I have DBeaver CE, which probably has that functionality. However, I've only ever used it to explore a local database. The DB built without errors, but the Deploy Logs show errors:
PostgreSQL Database directory appears to contain a database; Skipping initialization
2024-01-25 10:04:52.563 UTC [8] LOG: starting PostgreSQL 15.5 (Ubuntu 15.5-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
2024-01-25 10:04:52.564 UTC [8] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-01-25 10:04:52.564 UTC [8] LOG: listening on IPv6 address "::", port 5432
2024-01-25 10:04:52.571 UTC [8] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-01-25 10:04:52.578 UTC [35] LOG: database system was interrupted; last known up at 2024-01-25 08:03:23 UTC
2024-01-25 10:04:52.627 UTC [35] LOG: database system was not properly shut down; automatic recovery in progress
2024-01-25 10:04:52.631 UTC [35] LOG: redo starts at 0/2724FE0
2024-01-25 10:04:52.631 UTC [35] LOG: invalid record length at 0/27250C8: wanted 24, got 0
2024-01-25 10:04:52.631 UTC [35] LOG: redo done at 0/2725090 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2024-01-25 10:04:52.638 UTC [33] LOG: checkpoint starting: end-of-recovery immediate wait
2024-01-25 10:04:52.650 UTC [33] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.002 s, total=0.014 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB
2024-01-25 10:04:52.657 UTC [8] LOG: database system is ready to accept connections
2024-01-25 10:04:52.658 UTC [38] LOG: TimescaleDB background worker launcher connected to shared catalogs
PostgreSQL Database directory appears to contain a database; Skipping initialization
2024-01-25 10:04:52.563 UTC [8] LOG: starting PostgreSQL 15.5 (Ubuntu 15.5-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
2024-01-25 10:04:52.564 UTC [8] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-01-25 10:04:52.564 UTC [8] LOG: listening on IPv6 address "::", port 5432
2024-01-25 10:04:52.571 UTC [8] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-01-25 10:04:52.578 UTC [35] LOG: database system was interrupted; last known up at 2024-01-25 08:03:23 UTC
2024-01-25 10:04:52.627 UTC [35] LOG: database system was not properly shut down; automatic recovery in progress
2024-01-25 10:04:52.631 UTC [35] LOG: redo starts at 0/2724FE0
2024-01-25 10:04:52.631 UTC [35] LOG: invalid record length at 0/27250C8: wanted 24, got 0
2024-01-25 10:04:52.631 UTC [35] LOG: redo done at 0/2725090 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2024-01-25 10:04:52.638 UTC [33] LOG: checkpoint starting: end-of-recovery immediate wait
2024-01-25 10:04:52.650 UTC [33] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.002 s, total=0.014 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB
2024-01-25 10:04:52.657 UTC [8] LOG: database system is ready to accept connections
2024-01-25 10:04:52.658 UTC [38] LOG: TimescaleDB background worker launcher connected to shared catalogs
Brody
Brodyβ€’11mo ago
looks fine to me connect to the v2 database with dbeaver to confirm its accepting connections
guess_wh0
guess_wh0OPβ€’11mo ago
Do you know where to find the main info required to connect to the database? For example, it seems like I'm using an incorrect URL. I'll do my best and respond in about 1-2h, thank you for your help so far!
Brody
Brodyβ€’11mo ago
it would be in the variables tab of the v2 postgres service
guess_wh0
guess_wh0OPβ€’11mo ago
I managed to connect to the database! It seems to be working fine when navigating it in DBeaver --- I assume the errors I've been getting in Railway stem from something changing after redeploying the service that's using the DB
Brody
Brodyβ€’11mo ago
have you since redeployed your app?
guess_wh0
guess_wh0OPβ€’11mo ago
I have, but it's still producing errors:
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_..............
...
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection
Error: Rocket failed to launch due to failing fairings:
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
>> 'voteon_date_db' Database Pool
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_db`
>> timed out waiting for connection: connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
connection to server at "roundhouse.proxy.rlwy.net" (35.212.138.205), port 21689 failed: FATAL: sorry, too many clients already
Error: Rocket failed to launch due to failing fairings:
>> 'voteon_date_db' Database Pool
thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/rocket-0.5.0-rc.2/src/error.rs:229:17:
aborting due to fairing failure(s)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: database pool init error for pool named `voteon_date_..............
...
I tried locally rebuilding the project, and locally it's running just fine. Which makes me think that it's not an in-code thing, but something about the environment-variables getting messed up
Brody
Brodyβ€’11mo ago
postgres has a default max connection limit of 100, to me, this looks like your code is trying to open more than 100 connections?
guess_wh0
guess_wh0OPβ€’11mo ago
1. What about the errors makes you think that? 2. The code hasn't changed, and it used to work before, so I'm not sure whether this could be an issue.
Brody
Brodyβ€’11mo ago
1. because that's what the error is saying, too many clients 2. I know but that's all I can think of right now what environment variables does your code use to connect to the database?
guess_wh0
guess_wh0OPβ€’11mo ago
1. Ah, you're right. I read Error: database pool init error for pool named 'voteon_date_db' and somehow figured that it had issues initiating any connection. The two obvious ones are: (these are just my local values)
DATABASE_URL = postgres://postgres:postgres@localhost/voteon_date
ROCKET_DATABASES = {voteon_date_db={url="${DATABASE_URL}"}}
DATABASE_URL = postgres://postgres:postgres@localhost/voteon_date
ROCKET_DATABASES = {voteon_date_db={url="${DATABASE_URL}"}}
I thought there was also a PORT variable, but I can't find it at the moment. And these should be all that are required
Brody
Brodyβ€’11mo ago
the database url would include the port I think it's time to start digging into your code and how it's trying to open the connection
guess_wh0
guess_wh0OPβ€’11mo ago
The only thing I can find is that railway has Database Version: 15.5.0 while locally I have Database Version: 14.10.0. Do you know whether that could be an issue?
Brody
Brodyβ€’11mo ago
well the legacy database was postgres 13, but there has to be a deeper issue here, postgres isn't going to be telling you theres too many client connections for fun
guess_wh0
guess_wh0OPβ€’10mo ago
Update: 1. I managed to reproduce the bug locally by trying to connect to the railway-database via my script. 2. I still have no idea what I could be doing wrong. The good news is that now there's at least some more output...
Brody
Brodyβ€’10mo ago
I'd start to look at how your initialising your Postgres pool
milo
miloβ€’10mo ago
hey @guess_wh0, could you show me how you connect to the database or is rocket automatically picking it up? and, can you show me your cargo.toml please
guess_wh0
guess_wh0OPβ€’10mo ago
Hi, of course, I'm happy to provide the info you need! Cargo.toml:
[package]
edition = "2021"
name = "voteon_date"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4.19", features = ["serde"] }
diesel = { version = "1.4.8", features = ["postgres", "chrono", "serde_json"] }
diesel_migrations = "1.4.0"
dotenv = "0.15.0"
rand = "0.8.5"
serde = { version = "^1.0", features = ["derive"] }
ts-rs = { version = "6.2.0", features = ["chrono-impl", "format"] }
validator = { version = "0.16", features = ["derive"] }
# sentry = "0.25.0"


# Dependencies used when creating swagger-documentation
rocket_okapi = { version = "0.8.0-rc.2", features = [
"swagger",
"rocket_dyn_templates",
] }
schemars = { version = "0.8.10", features = ["chrono"] }


# Rocket
rocket = { version = "0.5.0-rc.2", features = ["json"] }

[dependencies.rocket_dyn_templates]
features = ["tera"]
version = "0.1.0-rc.2"

[dependencies.rocket_sync_db_pools]
default-features = false
features = ["diesel_postgres_pool"]
version = "0.1.0-rc.2"
[package]
edition = "2021"
name = "voteon_date"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4.19", features = ["serde"] }
diesel = { version = "1.4.8", features = ["postgres", "chrono", "serde_json"] }
diesel_migrations = "1.4.0"
dotenv = "0.15.0"
rand = "0.8.5"
serde = { version = "^1.0", features = ["derive"] }
ts-rs = { version = "6.2.0", features = ["chrono-impl", "format"] }
validator = { version = "0.16", features = ["derive"] }
# sentry = "0.25.0"


# Dependencies used when creating swagger-documentation
rocket_okapi = { version = "0.8.0-rc.2", features = [
"swagger",
"rocket_dyn_templates",
] }
schemars = { version = "0.8.10", features = ["chrono"] }


# Rocket
rocket = { version = "0.5.0-rc.2", features = ["json"] }

[dependencies.rocket_dyn_templates]
features = ["tera"]
version = "0.1.0-rc.2"

[dependencies.rocket_sync_db_pools]
default-features = false
features = ["diesel_postgres_pool"]
version = "0.1.0-rc.2"
milo
miloβ€’10mo ago
right so I’m guessing this is something diesel is doing I don’t know much about diesel, I’m going to look into it. please can I see how you intotialiss it, or do you just use the env variable?
guess_wh0
guess_wh0OPβ€’10mo ago
I use an environment variable. 1 sec, working on providing the rust-code (Reduced) main function: (Slightly outdated full version can be found here)
/// The analog to the usual `fn main()`.
#[launch]
fn rocket() -> _ {
// Automatically run db-migrations
let mut db_conn = database::establish_manual_connection();
db_conn
.run_pending_migrations(MIGRATIONS)
.unwrap_or_else(|e| panic!("Error running migrations: {}", e));

let mut rocket_build = rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())
.mount("/", api::dev_routes())
.mount("/swagger", make_swagger_ui(&api::open_api::get_docs()));

rocket_build
}
/// The analog to the usual `fn main()`.
#[launch]
fn rocket() -> _ {
// Automatically run db-migrations
let mut db_conn = database::establish_manual_connection();
db_conn
.run_pending_migrations(MIGRATIONS)
.unwrap_or_else(|e| panic!("Error running migrations: {}", e));

let mut rocket_build = rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())
.mount("/", api::dev_routes())
.mount("/swagger", make_swagger_ui(&api::open_api::get_docs()));

rocket_build
}
database::establish_manual_connection();: (Slightly outdated full version can be found here) (heavily inspired from docs-example)
/// Manually establishes a connection to the database, using [`diesel`]'s methods and the provided `.env`-file.
pub fn establish_manual_connection() -> PgConnection {
// Get variables from `.env`-file
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

println!("Trying to connect to {} ...", database_url);
PgConnection::establish(&database_url)
.unwrap_or_else(|e| panic!("\n\nError connecting to {database_url}: {e}\n\n"))
}
/// Manually establishes a connection to the database, using [`diesel`]'s methods and the provided `.env`-file.
pub fn establish_manual_connection() -> PgConnection {
// Get variables from `.env`-file
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

println!("Trying to connect to {} ...", database_url);
PgConnection::establish(&database_url)
.unwrap_or_else(|e| panic!("\n\nError connecting to {database_url}: {e}\n\n"))
}
The local environment-variables look like this. The second line can be used to try to connect to the railway-DB after inserting the password (and receive the error):
DATABASE_URL=postgres://postgres:postgres@localhost/voteon_date
#DATABASE_URL="postgresql://postgres:[password]@roundhouse.proxy.rlwy.net:21689/railway"
ROCKET_DATABASES={voteon_date_db={url="${DATABASE_URL}"}}
DATABASE_URL=postgres://postgres:postgres@localhost/voteon_date
#DATABASE_URL="postgresql://postgres:[password]@roundhouse.proxy.rlwy.net:21689/railway"
ROCKET_DATABASES={voteon_date_db={url="${DATABASE_URL}"}}
guess_wh0
guess_wh0OPβ€’10mo ago
--- By the way, the full source code can be found here. It's using slightly outdated packages (as opposed to the Cargo.toml i sent you), but it used to work just fine. Should you require any additional information, I'd be happy to provide it!
GitLab
Files Β· master Β· Glitchy-Tozier / voteon.date Β· GitLab
Check when your friends' free time matches!
milo
miloβ€’10mo ago
Awesome, gonna take a deeper look when home but this seems like it is not wiwtint for a connection to reply and just starting new ones. Do you have lots of migrations or something?
guess_wh0
guess_wh0OPβ€’10mo ago
There's 5 migrations if you include creating the database. However, all of them are >9 months old. There shouldn't be any new ones that require execution.
milo
miloβ€’10mo ago
Does it work if you run a local Postgres database?
guess_wh0
guess_wh0OPβ€’10mo ago
Yep, I'm able to connect to and use the local DB However, versions differ ↑
milo
miloβ€’10mo ago
What version is your local one? The message isn’t loading on my phone give me a break brody Oh right, finally, 14 I mean you could try that but in not sure how much success you’d have with it, worth a shot though maybe Downgrading to 14 on railway that is
guess_wh0
guess_wh0OPβ€’10mo ago
I could also see if i can manage to upgrade my local DB. And check if I then receive a local error. That'll take a few hours though since I need to get home first
milo
miloβ€’10mo ago
that would be good, take your time πŸ™‚
guess_wh0
guess_wh0OPβ€’10mo ago
Finally, after hours of fighting to first upgrade psql and then the actual database, I managed to upgrade to 15.5.0! (I'm new to this) However, everything the web-server still works when connecting to the local DB, while failing to connect to the online one.
No description
milo
miloβ€’10mo ago
Do you know if you are using TLS? In diesel
guess_wh0
guess_wh0OPβ€’10mo ago
I'm not sure. Any idea how to find out? The only thing I can find is that Rocket supports it. ChatGPT tells me this: However, Diesel itself doesn't directly handle TLS (Transport Layer Security) or encryption. The use of TLS in a Rust application using Diesel would typically depend on the database server and the connection settings you use when configuring your database connection. For example, if you are connecting to a PostgreSQL database using Diesel, the TLS or SSL connection would be configured through the PostgreSQL connection settings rather than directly within Diesel. You probably know best though what is and isn't possible. I've checked the API docs for diesel, but I don't recognize anything there. I tried disabling the migrations (see reducid code-sample) but unfortunately the error persists. Makes me think it's something else.
/// main function
fn rocket() -> _ {
// Automatically run db-migrations
let mut db_conn = database::establish_manual_connection();
/* db_conn
.run_pending_migrations(MIGRATIONS)
.unwrap_or_else(|e| panic!("Error running migrations: {}", e)); */

let mut rocket_build = rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())
.mount("/", api::dev_routes())
.mount("/swagger", make_swagger_ui(&api::open_api::get_docs()));

rocket_build
}
/// main function
fn rocket() -> _ {
// Automatically run db-migrations
let mut db_conn = database::establish_manual_connection();
/* db_conn
.run_pending_migrations(MIGRATIONS)
.unwrap_or_else(|e| panic!("Error running migrations: {}", e)); */

let mut rocket_build = rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())
.mount("/", api::dev_routes())
.mount("/swagger", make_swagger_ui(&api::open_api::get_docs()));

rocket_build
}
Brody
Brodyβ€’10mo ago
postgres has a a default max connection limit of 100, something you are doing is trying to open more than that
milo
miloβ€’10mo ago
try not using manual connection?\
guess_wh0
guess_wh0OPβ€’10mo ago
I get what you're saying, but haven't yet found out what could be the issue. The main function (see above) is the only place where I open a connection, and it's only called once. Of course, there may be some dark magic going on in the background that I don't know about.
milo
miloβ€’10mo ago
how many times is the rocket ufnction being called?
guess_wh0
guess_wh0OPβ€’10mo ago
Only once. It's basically the main()-function of the server. See last reply to @Brody .
milo
miloβ€’10mo ago
im thinking it might accidentaly being called more then that
guess_wh0
guess_wh0OPβ€’10mo ago
I did, but that cuts off all database-functionality, even with my local DB. That code is directly copied from Diesel's Getting Started guide by diesel's code, so I don't think I've made an error there.
milo
miloβ€’10mo ago
hmmmm
guess_wh0
guess_wh0OPβ€’10mo ago
See this comment for what the establish_manual_connection()-function looks like.
milo
miloβ€’10mo ago
can you add some logs in the rockejt function to see how many times the function is actually running
guess_wh0
guess_wh0OPβ€’10mo ago
I did, it's run only once. It seems to be async though – or the error is occuring later – as ALL print-statements are executed, even the ones at the very end of the function.
milo
miloβ€’10mo ago
wtf, this is why i hate diesel anguish
guess_wh0
guess_wh0OPβ€’10mo ago
Oh right, here's the full backtrace. The part of my code it highlights (voteon_date/backend/src/main.rs:53:1) doesn't seem particularly helpful. That line is just the closing bracket of the rocket() (=main) function.
guess_wh0
guess_wh0OPβ€’10mo ago
I feel the same way πŸ˜…
milo
miloβ€’10mo ago
all this tells me is that it is coming from the main function so like
guess_wh0
guess_wh0OPβ€’10mo ago
Okaaaay, so after some digging, it seems .attach(DbConn::fairing()) is the problematic line (see rocket()-function). However, I don't see where I'm doing anything dangerous. DbConn is just a rocket guard type and looks exactly like it's shown in the docs.
/// Connection to the server's [diesel]-PostgreSQL-database.
#[database("voteon_date_db")]
pub struct DbConn(diesel::PgConnection);
/// Connection to the server's [diesel]-PostgreSQL-database.
#[database("voteon_date_db")]
pub struct DbConn(diesel::PgConnection);
(Plus, I've added some methods to the struct. However, these 1. do not generate new connections and 2. aren't called until a route is requested.) It feels strange that I'm able to connect to my local DB but not the Railway one. Could it be that somehow they're set up differently? Allowing/disallowing different things?
Brody
Brodyβ€’10mo ago
it's just a standard postgres image, and according to postgres docs the max concurrent connection limit is 100, and it's not going to tell you you've hit that just for fun, this is 100% a code issue
guess_wh0
guess_wh0OPβ€’10mo ago
I understand that. The strange thing is that: 1. it used to work just fine and I haven't changed anything about my setup (though maybe the package-manager did some cheating) 2. it still works with my local DB. This leads me to believe that the code isn't just straight out wrong in every regard but that there has to be some interaction that results in this particular issue.
milo
miloβ€’10mo ago
yeah, unfortunately this doesn't look like it is a issue with postgres or railway, and most likely something to do with diesel. im not as experienced with diesel, but i can recommend sqlx to be a solid replacement. much more reliable in my experience
sqlx - Rust
The async SQL toolkit for Rust, built with ❀️ by the LaunchBadge team.
Brody
Brodyβ€’10mo ago
going to second what milo said, this isn't going to be an issue with postgres or railway, and running locally hardly ever means it will run perfectly elsewhere, this is a code issue unfortunately, the problematic interaction is that your app is opening too many connections, find a way to stop that behaviour and your problem is solved, but I would still take milo's advice and switch to sqlx
guess_wh0
guess_wh0OPβ€’10mo ago
Well that's unfortunate as it'll mean a lot of rewriting. However, if it's the main way to get the app running again, I'll have to do it. Thank you for your suggestion regarding sqlx. I'll perform 1-2 more tests in hopes of finding the bug somewhere, but if they turn out unhelpful, I'll work on replacing diesel with sqlx. I'll write an update when there's new information The website's back up!! πŸŽ‰
Solution
guess_wh0
guess_wh0β€’10mo ago
Actually, it turns out that the crash was caused for different reasons at different places. 1. Rocket seems to decide how many connections to create. More specifically, rocket_sync_db_pools. That package "defaults to the configured number of workers * 4", which seems to be >100 on railway. I assume "workers" means "CPUs", but feel free to correct me if you know more than I do πŸ™‚ Thus, on Railway, there were simply too many connections. 2. Locally, when trying to connect to the Railway-DB, my code only tries to create 32 connections (maybe because of 8 CPUs?). - Thus, I was always able to connect to my local DB - However, connecting to the Railway-DB failed because creating 32 connections within the default timeout of 5s wasn't possible. - Surprisingly, diesel didn't do anything wrong this time (at least according to my understanding) - Limiting pool_size and timeout allowed me to connect again.
guess_wh0
guess_wh0OPβ€’10mo ago
--- Question to you experts: Is there a certain value for pool_size (= nr of connections) you would recommend to use? Why?
Brody
Brodyβ€’10mo ago
if "workers" means cpu cores, then thats 32 * 4 = 128 connections yep too many pooled connections respectfully, you could have saved a lot of time if you had taken my advice but to answer your question, a pool size of 10 is fine for most scenarios
Want results from more Discord servers?
Add your server