@@ -328,11 +328,14 @@ func UploadSlice(ctx context.Context, storage driver.Driver, req *reqres.UploadS
328
328
var msu * sliceup
329
329
var err error
330
330
331
- sa , ok := sliceupMap .Load (req .UploadID )
332
- if ! ok {
331
+ // 使用 LoadOrStore 避免并发竞态条件
332
+ sa , loaded := sliceupMap .LoadOrStore (req .UploadID , nil )
333
+ if ! loaded {
334
+ // 首次加载,需要从数据库获取
333
335
su , e := db .GetSliceUpload (map [string ]any {"id" : req .UploadID })
334
336
if e != nil {
335
337
log .Errorf ("failed get slice upload [%d]: %+v" , req .UploadID , e )
338
+ sliceupMap .Delete (req .UploadID ) // 清理无效的 key
336
339
return e
337
340
}
338
341
msu = & sliceup {
@@ -346,7 +349,9 @@ func UploadSlice(ctx context.Context, storage driver.Driver, req *reqres.UploadS
346
349
if err != nil {
347
350
msu .Status = tables .SliceUploadStatusFailed
348
351
msu .Message = err .Error ()
349
- db .UpdateSliceUpload (msu .SliceUpload )
352
+ if updateErr := db .UpdateSliceUpload (msu .SliceUpload ); updateErr != nil {
353
+ log .Errorf ("Failed to update slice upload status: %v" , updateErr )
354
+ }
350
355
}
351
356
}()
352
357
@@ -476,10 +481,20 @@ func SliceUpComplete(ctx context.Context, storage driver.Driver, uploadID uint)
476
481
if err != nil {
477
482
msu .Status = tables .SliceUploadStatusFailed
478
483
msu .Message = err .Error ()
479
- db .UpdateSliceUpload (msu .SliceUpload )
484
+ if updateErr := db .UpdateSliceUpload (msu .SliceUpload ); updateErr != nil {
485
+ log .Errorf ("Failed to update slice upload status: %v" , updateErr )
486
+ }
480
487
}
488
+ // 确保资源清理
481
489
if msu .tmpFile != nil {
482
- msu .tmpFile .Close ()
490
+ if closeErr := msu .tmpFile .Close (); closeErr != nil {
491
+ log .Errorf ("Failed to close tmp file: %v" , closeErr )
492
+ }
493
+ }
494
+ if msu .TmpFile != "" {
495
+ if removeErr := os .Remove (msu .TmpFile ); removeErr != nil && ! os .IsNotExist (removeErr ) {
496
+ log .Errorf ("Failed to remove tmp file %s: %v" , msu .TmpFile , removeErr )
497
+ }
483
498
}
484
499
sliceupMap .Delete (msu .ID )
485
500
@@ -499,9 +514,15 @@ func SliceUpComplete(ctx context.Context, storage driver.Driver, uploadID uint)
499
514
}
500
515
// 清理缓存及临时文件
501
516
if msu .tmpFile != nil {
502
- msu .tmpFile .Close ()
517
+ if closeErr := msu .tmpFile .Close (); closeErr != nil {
518
+ log .Errorf ("Failed to close tmp file: %v" , closeErr )
519
+ }
520
+ }
521
+ if msu .TmpFile != "" {
522
+ if removeErr := os .Remove (msu .TmpFile ); removeErr != nil && ! os .IsNotExist (removeErr ) {
523
+ log .Errorf ("Failed to remove tmp file %s: %v" , msu .TmpFile , removeErr )
524
+ }
503
525
}
504
- os .Remove (msu .TmpFile )
505
526
506
527
return rsp , nil
507
528
@@ -547,7 +568,11 @@ func SliceUpComplete(ctx context.Context, storage driver.Driver, uploadID uint)
547
568
log .Error ("Put error" , msu .SliceUpload , err )
548
569
return nil , err
549
570
}
550
- os .Remove (msu .TmpFile )
571
+ if msu .TmpFile != "" {
572
+ if removeErr := os .Remove (msu .TmpFile ); removeErr != nil && ! os .IsNotExist (removeErr ) {
573
+ log .Errorf ("Failed to remove tmp file %s: %v" , msu .TmpFile , removeErr )
574
+ }
575
+ }
551
576
return & reqres.UploadSliceCompleteResp {
552
577
Complete : 1 ,
553
578
UploadID : msu .ID ,
0 commit comments