Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential race condition in closures #23

Closed
krugazor opened this issue Jun 29, 2017 · 2 comments
Closed

Potential race condition in closures #23

krugazor opened this issue Jun 29, 2017 · 2 comments

Comments

@krugazor
Copy link

Context: a very large postgresql db (> 100k hits per query), a function that queries for the existence of 2 conditions, then inserts data if the conditions aren't met.

Expected result: no error, insertion

Actual result: no error (TUPLE_OK from the pq lib, successNoData from Kuery), no insertion

More details:

the basic layout of the code was:

query { result in
  query { result in {
    insert
  }
}

What did work was putting the insert on a separate connection from the pool.

let conn = pool.getConnection() 
conn.query { result in {
  conn.query { result in {
    let conn2 = pool.getConnection()
    conn2.insert
  }
}

After a conversation with @shmuelk, I opened this issue, but I'm aware of the difficulties reproducing such a bug with access to my exact setup.

irar2 added a commit that referenced this issue Jul 2, 2017
@irar2
Copy link
Contributor

irar2 commented Jul 2, 2017

The problem here is that when doing

query1 { result1 in
  query2 { result2 in {
    insert
  }
}

query2 is executed when the connection is in the middle of reading the results of query1, i.e. the call to PQgetResult(connection) still gets the next row of query1 even in the second callback for result2.asResultSet.nextRow().

We have to decide what to do in such situations, when we are in the middle of fetching results and receive an 'execute' call with another query. We can either kill the first query fetcher, or don't execute the second query and return an error for it.

The branch issue_23 contains the first option. It adds a state to PostgreSQLConnection, which is checked before query execution, and kills the existing fetcher in case the state is .fetchingResultSet.

@djones6
Copy link
Contributor

djones6 commented Mar 14, 2018

The fix has been merged in #45 and tagged 1.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants