Skip to content

Commit

Permalink
Fix to #761, preventing incorrect disposed command object
Browse files Browse the repository at this point in the history
This addresses a race condition where a new PreparedSqliteInsertCommand object was returned in a disposed state if a second object was created and inserted into the command map during its creation. The fix always returns the item inserted into the command map.
  • Loading branch information
ElteHupkes committed Aug 29, 2019
1 parent 91c17ad commit 52eb23c
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,21 +1710,20 @@ PreparedSqlLiteInsertCommand GetInsertCommand (TableMapping map, string extra)
var key = Tuple.Create (map.MappedType.FullName, extra);

lock (_insertCommandMap) {
_insertCommandMap.TryGetValue (key, out prepCmd);
if (_insertCommandMap.TryGetValue (key, out prepCmd)) {
return prepCmd;
}
}

if (prepCmd == null) {
prepCmd = CreateInsertCommand (map, extra);
var added = false;
lock (_insertCommandMap) {
if (!_insertCommandMap.ContainsKey (key)) {
_insertCommandMap.Add (key, prepCmd);
added = true;
}
}
if (!added) {
prepCmd = CreateInsertCommand (map, extra);

lock (_insertCommandMap) {
if (_insertCommandMap.TryGetValue (key, out var existing)) {
prepCmd.Dispose ();
return existing;
}

_insertCommandMap.Add (key, prepCmd);
}

return prepCmd;
Expand Down

0 comments on commit 52eb23c

Please sign in to comment.