@@ -1160,17 +1160,26 @@ async Task<NpgsqlBinaryImporter> BeginBinaryImport(bool async, string copyFromCo
11601160 LogMessages . StartingBinaryImport ( connector . LoggingConfiguration . CopyLogger , connector . Id ) ;
11611161 // no point in passing a cancellationToken here, as we register the cancellation in the Init method
11621162 connector . StartUserAction ( ConnectorState . Copy , attemptPgCancellation : false ) ;
1163+ var importer = new NpgsqlBinaryImporter ( connector ) ;
11631164 try
11641165 {
1165- var importer = new NpgsqlBinaryImporter ( connector ) ;
11661166 await importer . Init ( copyFromCommand , async , cancellationToken ) . ConfigureAwait( false ) ;
11671167 connector . CurrentCopyOperation = importer ;
11681168 return importer ;
11691169 }
11701170 catch
11711171 {
1172- connector . EndUserAction ( ) ;
1173- EndBindingScope ( ConnectorBindingScope . Copy ) ;
1172+ try
1173+ {
1174+ if ( async)
1175+ await importer . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1176+ else
1177+ importer . Dispose ( ) ;
1178+ }
1179+ catch
1180+ {
1181+ // ignored
1182+ }
11741183 throw ;
11751184 }
11761185 }
@@ -1210,17 +1219,26 @@ async Task<NpgsqlBinaryExporter> BeginBinaryExport(bool async, string copyToComm
12101219 LogMessages . StartingBinaryExport ( connector . LoggingConfiguration . CopyLogger , connector . Id ) ;
12111220 // no point in passing a cancellationToken here, as we register the cancellation in the Init method
12121221 connector . StartUserAction ( ConnectorState . Copy , attemptPgCancellation : false ) ;
1222+ var exporter = new NpgsqlBinaryExporter ( connector ) ;
12131223 try
12141224 {
1215- var exporter = new NpgsqlBinaryExporter ( connector ) ;
12161225 await exporter . Init ( copyToCommand , async , cancellationToken ) . ConfigureAwait( false ) ;
12171226 connector . CurrentCopyOperation = exporter ;
12181227 return exporter ;
12191228 }
12201229 catch
12211230 {
1222- connector . EndUserAction ( ) ;
1223- EndBindingScope ( ConnectorBindingScope . Copy ) ;
1231+ try
1232+ {
1233+ if ( async)
1234+ await exporter . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1235+ else
1236+ exporter . Dispose ( ) ;
1237+ }
1238+ catch
1239+ {
1240+ // ignored
1241+ }
12241242 throw ;
12251243 }
12261244 }
@@ -1266,18 +1284,27 @@ async Task<TextWriter> BeginTextImport(bool async, string copyFromCommand, Cance
12661284 LogMessages . StartingTextImport ( connector . LoggingConfiguration . CopyLogger , connector . Id ) ;
12671285 // no point in passing a cancellationToken here, as we register the cancellation in the Init method
12681286 connector . StartUserAction ( ConnectorState . Copy , attemptPgCancellation : false ) ;
1287+ var copyStream = new NpgsqlRawCopyStream ( connector ) ;
12691288 try
12701289 {
1271- var copyStream = new NpgsqlRawCopyStream ( connector ) ;
12721290 await copyStream . Init ( copyFromCommand , async , cancellationToken ) . ConfigureAwait( false ) ;
12731291 var writer = new NpgsqlCopyTextWriter ( connector , copyStream ) ;
12741292 connector . CurrentCopyOperation = writer ;
12751293 return writer ;
12761294 }
12771295 catch
12781296 {
1279- connector . EndUserAction ( ) ;
1280- EndBindingScope ( ConnectorBindingScope . Copy ) ;
1297+ try
1298+ {
1299+ if ( async)
1300+ await copyStream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1301+ else
1302+ copyStream . Dispose ( ) ;
1303+ }
1304+ catch
1305+ {
1306+ // ignored
1307+ }
12811308 throw ;
12821309 }
12831310 }
@@ -1323,18 +1350,27 @@ async Task<TextReader> BeginTextExport(bool async, string copyToCommand, Cancell
13231350 LogMessages . StartingTextExport ( connector . LoggingConfiguration . CopyLogger , connector . Id ) ;
13241351 // no point in passing a cancellationToken here, as we register the cancellation in the Init method
13251352 connector . StartUserAction ( ConnectorState . Copy , attemptPgCancellation : false ) ;
1353+ var copyStream = new NpgsqlRawCopyStream ( connector ) ;
13261354 try
13271355 {
1328- var copyStream = new NpgsqlRawCopyStream ( connector ) ;
13291356 await copyStream . Init ( copyToCommand , async , cancellationToken ) . ConfigureAwait( false ) ;
13301357 var reader = new NpgsqlCopyTextReader ( connector , copyStream ) ;
13311358 connector . CurrentCopyOperation = reader ;
13321359 return reader ;
13331360 }
13341361 catch
13351362 {
1336- connector . EndUserAction ( ) ;
1337- EndBindingScope ( ConnectorBindingScope . Copy ) ;
1363+ try
1364+ {
1365+ if ( async)
1366+ await copyStream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1367+ else
1368+ copyStream . Dispose ( ) ;
1369+ }
1370+ catch
1371+ {
1372+ // ignored
1373+ }
13381374 throw ;
13391375 }
13401376 }
@@ -1380,9 +1416,9 @@ async Task<NpgsqlRawCopyStream> BeginRawBinaryCopy(bool async, string copyComman
13801416 LogMessages . StartingRawCopy ( connector . LoggingConfiguration . CopyLogger , connector . Id ) ;
13811417 // no point in passing a cancellationToken here, as we register the cancellation in the Init method
13821418 connector . StartUserAction ( ConnectorState . Copy , attemptPgCancellation : false ) ;
1419+ var stream = new NpgsqlRawCopyStream ( connector ) ;
13831420 try
13841421 {
1385- var stream = new NpgsqlRawCopyStream ( connector ) ;
13861422 await stream . Init ( copyCommand , async , cancellationToken ) . ConfigureAwait( false ) ;
13871423 if ( ! stream . IsBinary )
13881424 {
@@ -1395,8 +1431,17 @@ async Task<NpgsqlRawCopyStream> BeginRawBinaryCopy(bool async, string copyComman
13951431 }
13961432 catch
13971433 {
1398- connector . EndUserAction ( ) ;
1399- EndBindingScope ( ConnectorBindingScope . Copy ) ;
1434+ try
1435+ {
1436+ if ( async)
1437+ await stream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
1438+ else
1439+ stream . Dispose ( ) ;
1440+ }
1441+ catch
1442+ {
1443+ // ignored
1444+ }
14001445 throw ;
14011446 }
14021447 }
0 commit comments