Repository: dbt-labs/dbt-core

Issue:
[CT-2310] [Bug] The test_no_introspect test fails intermittently
The Tests and Code Checks action has been failing on the main branch of dbt-core due to intermittent failure of the test_no_introspect test. We need to improve the reliablity of this test.

Initial investigation uncovered log lines suggesting that the issue may be related to closing an already-closed connection:

```
[DEBUG] file_log: 14:50:53.859407 [debug] [Thread-1  ]: Traceback (most recent call last):
  File "/home/runner/work/dbt-core/dbt-core/plugins/postgres/dbt/adapters/postgres/connections.py", line 66, in exception_handler
    yield
  File "/home/runner/work/dbt-core/dbt-core/core/dbt/adapters/sql/connections.py", line 80, in add_query
    cursor = connection.handle.cursor()
psycopg2.InterfaceError: connection already closed
```

However, there is another issue, which is that the resulting DbtRuntimeException is constructed with the wrong type in postgres/connections.py at line 89. The parameter is an Exception rather than a str:

```python
raise dbt.exceptions.DbtRuntimeError(e) from e
```

As a result, a secondary exception is generated during exception handling.
[CT-2310] [Bug] The test_no_introspect test fails intermittently
The Tests and Code Checks action has been failing on the main branch of dbt-core due to intermittent failure of the test_no_introspect test. We need to improve the reliablity of this test.

Initial investigation uncovered log lines suggesting that the issue may be related to closing an already-closed connection:

```
[DEBUG] file_log: 14:50:53.859407 [debug] [Thread-1  ]: Traceback (most recent call last):
  File "/home/runner/work/dbt-core/dbt-core/plugins/postgres/dbt/adapters/postgres/connections.py", line 66, in exception_handler
    yield
  File "/home/runner/work/dbt-core/dbt-core/core/dbt/adapters/sql/connections.py", line 80, in add_query
    cursor = connection.handle.cursor()
psycopg2.InterfaceError: connection already closed
```

However, there is another issue, which is that the resulting DbtRuntimeException is constructed with the wrong type in postgres/connections.py at line 89. The parameter is an Exception rather than a str:

```python
raise dbt.exceptions.DbtRuntimeError(e) from e
```

As a result, a secondary exception is generated during exception handling.


Discussion:
Was this really resolved by #7208? Or did we just mark the test to skip?

https://github.com/dbt-labs/dbt-core/pull/7208/files#r1151279272

I'm going to reopen this and add it to the Flaky Tests epic
@emmyoop Will run these tests through the flakey tester to determine 1. if they are still flakey and 2. how flakey.
If they are pretty flakey we will try to address this sprint. If not we will punt to next sprint.
https://github.com/dbt-labs/dbt-core/actions/runs/5671681780
This test is marked to be skipped on `main`.  It's not flaky.  It failed 1000/1000 runs once I removed the skip.  
Was this really resolved by #7208? Or did we just mark the test to skip?

https://github.com/dbt-labs/dbt-core/pull/7208/files#r1151279272

I'm going to reopen this and add it to the Flaky Tests epic
@emmyoop Will run these tests through the flakey tester to determine 1. if they are still flakey and 2. how flakey.
If they are pretty flakey we will try to address this sprint. If not we will punt to next sprint.
https://github.com/dbt-labs/dbt-core/actions/runs/5671681780
This test is marked to be skipped on `main`.  It's not flaky.  It failed 1000/1000 runs once I removed the skip.  

Resolving patch:
diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py
--- a/core/dbt/exceptions.py
+++ b/core/dbt/exceptions.py
@@ -486,7 +486,7 @@ def __init__(self, thread_id, known: List):
         self.thread_id = thread_id
         self.known = known
         super().__init__(
-            msg="connection never acquired for thread {self.thread_id}, have {self.known}"
+            msg=f"connection never acquired for thread {self.thread_id}, have {self.known}"
         )
 
 


Test patch:
diff --git a/tests/functional/compile/test_compile.py b/tests/functional/compile/test_compile.py
--- a/tests/functional/compile/test_compile.py
+++ b/tests/functional/compile/test_compile.py
@@ -49,9 +49,8 @@ def test_default(self, project):
         assert get_lines("first_model") == ["select 1 as fun"]
         assert any("_test_compile as schema" in line for line in get_lines("second_model"))
 
-    @pytest.mark.skip("Investigate flaky test #7179")
     def test_no_introspect(self, project):
-        with pytest.raises(DbtRuntimeError):
+        with pytest.raises(DbtRuntimeError, match="connection never acquired for thread"):
             run_dbt(["compile", "--no-introspect"])