# Pool Timeout TypeError Fix

## Problem

Production logs showed 5 occurrences of:
```
TypeError: connect() got an unexpected keyword argument 'pool_timeout'
```

## Root Cause

The `pool_timeout` parameter is **only supported by `aiomysql`**, not by `mysql-connector-python`.

### Supported Parameters by Library:

| Parameter | `aiomysql.create_pool()` | `mysql.connector.connect()` | `MySQLConnectionPool()` |
|-----------|-------------------------|----------------------------|------------------------|
| `pool_timeout` | ✅ Yes | ❌ No | ❌ No |
| `connect_timeout` | ✅ Yes | ✅ Yes (connection-level) | ❌ No (pool-level) |
| `pool_size` | ✅ Yes (as `maxsize`) | ❌ No | ✅ Yes |

### The Bug

`MySQLConnectionPool()` accepts `**kwargs` and passes them through to the underlying `mysql.connector.connect()` calls. If `pool_timeout` is accidentally passed to `MySQLConnectionPool()`, it will cause a TypeError when creating connections.

## Investigation Results

### Files Checked:

1. **`/ws/database_async.py`** ✅ CORRECT
   - Line 56: `pool_timeout=30` correctly used with `aiomysql.create_pool()`
   - This is the ONLY place where `pool_timeout` appears in the codebase

2. **`/ws/database/db_operations.py`** ✅ CORRECT (now defensive)
   - Line 144: `mysql.connector.connect()` - does NOT include `pool_timeout`
   - Added explicit parameter dict and documentation

3. **`/ws/database.py`** ✅ CORRECT (now defensive)
   - Line 53: `MySQLConnectionPool()` - does NOT include `pool_timeout`
   - Added defensive filtering of unsupported parameters

4. **`/ws/database/transactions.py`** ✅ CORRECT
   - Line 46: `mysql.connector.connect()` - does NOT include `pool_timeout`

### Where the Bug Might Be

Since the bug isn't in the local codebase, it's likely:

1. **Production-only code path** not present in local files
2. **Cached/old code in production** that hasn't been redeployed
3. **Environment variable** accidentally adding `pool_timeout` to config
4. **Third-party library** or monkey-patch adding the parameter

## The Fix

Added defensive programming to prevent this error:

### 1. `/ws/database.py` (Sync Connection Pool)
```python
# Build connection parameters explicitly
conn_params = {
    'pool_name': "baolife_pool",
    'pool_size': min(config.MAX_CONNECTIONS, 32),
    'pool_reset_session': True,
    'host': config.DB_HOST,
    'port': config.DB_PORT,
    'user': config.DB_USER,
    'password': config.DB_PASSWORD,
    'database': config.DB_NAME
}

# Filter out any unsupported parameters
unsupported_params = ['pool_timeout', 'connect_timeout']
conn_params = {k: v for k, v in conn_params.items() if k not in unsupported_params}

_connection_pool = pooling.MySQLConnectionPool(**conn_params)
```

### 2. `/ws/database/db_operations.py` (Direct Connections)
```python
# Build connection parameters explicitly
conn_params = {
    'host': config.DB_HOST,
    'port': config.DB_PORT,
    'user': config.DB_USER,
    'password': config.DB_PASSWORD,
    'database': config.DB_NAME
}

# NOTE: Do NOT add pool_timeout here - it's only for aiomysql
return mysql.connector.connect(**conn_params)
```

## Verification

After deploying this fix:

1. The defensive parameter filtering will prevent `pool_timeout` from being passed to sync connections
2. If the parameter somehow appears in config, it will be silently filtered out
3. The async pool (`aiomysql`) will continue to use `pool_timeout` correctly

## Deployment Notes

1. Deploy the updated code to production
2. Monitor logs for the TypeError - it should disappear
3. If errors persist, check for:
   - Environment variables adding unexpected parameters
   - Third-party code calling database functions with kwargs
   - Monkey patches or dynamic parameter injection

## Future Prevention

**Best Practices:**

1. **Never** add `pool_timeout` to `mysql.connector.connect()` or `MySQLConnectionPool()`
2. **Only** use `pool_timeout` with `aiomysql.create_pool()`
3. Keep async database code (`database_async.py`) separate from sync code
4. Use explicit parameter dicts instead of `**kwargs` where possible
5. Add type hints and parameter validation for database functions

## Related Files

- `/ws/database_async.py` - Async database layer (uses `aiomysql` with `pool_timeout`)
- `/ws/database.py` - Sync connection pool (uses `mysql-connector-python`, no `pool_timeout`)
- `/ws/database/db_operations.py` - Sync database operations (no `pool_timeout`)
- `/ws/config.py` - Configuration (doesn't include `pool_timeout`)
