@@ -46,5 +46,61 @@ def is_stopped(self) -> bool:
4646 def generate_name (self ) -> str :
4747 return "-" .join ((self .kind , secrets .token_hex (10 )))
4848
49+ def disable_connection_pooling (self ):
50+ """
51+ Disable connection pooling for steady_queue processes.
52+
53+ Connection pooling with psycopg doesn't work with forked processes.
54+ This method removes pool configuration from database settings to prevent
55+ pool-related errors in steady_queue workers.
56+ """
57+ import logging
58+
59+ from django .conf import settings
60+
61+ logger = logging .getLogger ("steady_queue" )
62+
63+ # Disable pooling in database configuration
64+ if hasattr (settings , "DATABASES" ):
65+ for alias , db_config in settings .DATABASES .items ():
66+ if db_config .get ("ENGINE" ) == "django.db.backends.postgresql" :
67+ # Remove pool configuration if it exists
68+ options = db_config .setdefault ("OPTIONS" , {})
69+ if "pool" in options :
70+ logger .info (
71+ "PID %d disabling connection pooling for database '%s'" ,
72+ os .getpid (),
73+ alias ,
74+ )
75+ del options ["pool" ]
76+
77+ # Also disable on any existing connections
78+ for alias in connections :
79+ connection = connections [alias ]
80+ if hasattr (connection , "pool" ) and connection .pool is not None :
81+ try :
82+ connection .pool .close ()
83+ connection .pool = None
84+ logger .debug (
85+ "PID %d removed existing pool for '%s'" , os .getpid (), alias
86+ )
87+ except Exception as e :
88+ logger .debug (
89+ "PID %d failed to close existing pool for '%s': %s" ,
90+ os .getpid (),
91+ alias ,
92+ e ,
93+ )
94+
4995 def reset_database_connections (self ):
96+ """
97+ Reset database connections for forked processes.
98+
99+ This disables connection pooling and resets connection state to prevent
100+ issues with shared connections between parent and child processes.
101+ """
102+ # First disable connection pooling for steady_queue processes
103+ self .disable_connection_pooling ()
104+
105+ # Close all existing connections
50106 connections .close_all ()
0 commit comments