Skip to content

Commit

Permalink
only try to cancel open connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Jul 27, 2020
1 parent fd0b460 commit 23ed2f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions core/dbt/adapters/sql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import dbt.clients.agate_helper
import dbt.exceptions
from dbt.contracts.connection import Connection
from dbt.contracts.connection import Connection, ConnectionState
from dbt.adapters.base import BaseConnectionManager
from dbt.logger import GLOBAL_LOGGER as logger

Expand Down Expand Up @@ -37,7 +37,10 @@ def cancel_open(self) -> List[str]:

# if the connection failed, the handle will be None so we have
# nothing to cancel.
if connection.handle is not None:
if (
connection.handle is not None and
connection.state == ConnectionState.OPEN
):
self.cancel(connection)
if connection.name is not None:
names.append(connection.name)
Expand Down
12 changes: 11 additions & 1 deletion plugins/postgres/dbt/adapters/postgres/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ def open(cls, connection):

def cancel(self, connection):
connection_name = connection.name
pid = connection.handle.get_backend_pid()
try:
pid = connection.handle.get_backend_pid()
except psycopg2.InterfaceError as exc:
# if the connection is already closed, not much to cancel!
if 'already closed' in str(exc):
logger.debug(
f'Connection {connection_name} was already closed'
)
return
# probably bad, re-raise it
raise

sql = "select pg_terminate_backend({})".format(pid)

Expand Down

0 comments on commit 23ed2f5

Please sign in to comment.