Skip to content

Commit 52eb23c

Browse files
committed
Fix to #761, preventing incorrect disposed command object
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.
1 parent 91c17ad commit 52eb23c

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/SQLite.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,21 +1710,20 @@ PreparedSqlLiteInsertCommand GetInsertCommand (TableMapping map, string extra)
17101710
var key = Tuple.Create (map.MappedType.FullName, extra);
17111711

17121712
lock (_insertCommandMap) {
1713-
_insertCommandMap.TryGetValue (key, out prepCmd);
1713+
if (_insertCommandMap.TryGetValue (key, out prepCmd)) {
1714+
return prepCmd;
1715+
}
17141716
}
17151717

1716-
if (prepCmd == null) {
1717-
prepCmd = CreateInsertCommand (map, extra);
1718-
var added = false;
1719-
lock (_insertCommandMap) {
1720-
if (!_insertCommandMap.ContainsKey (key)) {
1721-
_insertCommandMap.Add (key, prepCmd);
1722-
added = true;
1723-
}
1724-
}
1725-
if (!added) {
1718+
prepCmd = CreateInsertCommand (map, extra);
1719+
1720+
lock (_insertCommandMap) {
1721+
if (_insertCommandMap.TryGetValue (key, out var existing)) {
17261722
prepCmd.Dispose ();
1723+
return existing;
17271724
}
1725+
1726+
_insertCommandMap.Add (key, prepCmd);
17281727
}
17291728

17301729
return prepCmd;

0 commit comments

Comments
 (0)