@@ -4313,8 +4313,11 @@ func (mb *msgBlock) eraseMsg(seq uint64, ri, rl int) error {
4313
4313
4314
4314
// Truncate this message block to the storedMsg.
4315
4315
func (mb * msgBlock ) truncate (sm * StoreMsg ) (nmsgs , nbytes uint64 , err error ) {
4316
+ mb .mu .Lock ()
4317
+ defer mb .mu .Unlock ()
4318
+
4316
4319
// Make sure we are loaded to process messages etc.
4317
- if err := mb .loadMsgs (); err != nil {
4320
+ if err := mb .loadMsgsWithLock (); err != nil {
4318
4321
return 0 , 0 , err
4319
4322
}
4320
4323
@@ -4328,8 +4331,6 @@ func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) {
4328
4331
4329
4332
var purged , bytes uint64
4330
4333
4331
- mb .mu .Lock ()
4332
-
4333
4334
checkDmap := mb .dmap .Size () > 0
4334
4335
var smv StoreMsg
4335
4336
@@ -4365,28 +4366,24 @@ func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) {
4365
4366
if mb .cmp != NoCompression {
4366
4367
buf , err := mb .loadBlock (nil )
4367
4368
if err != nil {
4368
- mb .mu .Unlock ()
4369
4369
return 0 , 0 , fmt .Errorf ("failed to load block from disk: %w" , err )
4370
4370
}
4371
4371
if mb .bek != nil && len (buf ) > 0 {
4372
4372
bek , err := genBlockEncryptionKey (mb .fs .fcfg .Cipher , mb .seed , mb .nonce )
4373
4373
if err != nil {
4374
- mb .mu .Unlock ()
4375
4374
return 0 , 0 , err
4376
4375
}
4377
4376
mb .bek = bek
4378
4377
mb .bek .XORKeyStream (buf , buf )
4379
4378
}
4380
4379
buf , err = mb .decompressIfNeeded (buf )
4381
4380
if err != nil {
4382
- mb .mu .Unlock ()
4383
4381
return 0 , 0 , fmt .Errorf ("failed to decompress block: %w" , err )
4384
4382
}
4385
4383
buf = buf [:eof ]
4386
4384
copy (mb .lchk [0 :], buf [:len (buf )- checksumSize ])
4387
4385
buf , err = mb .cmp .Compress (buf )
4388
4386
if err != nil {
4389
- mb .mu .Unlock ()
4390
4387
return 0 , 0 , fmt .Errorf ("failed to recompress block: %w" , err )
4391
4388
}
4392
4389
meta := & CompressionInfo {
@@ -4397,19 +4394,16 @@ func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) {
4397
4394
if mb .bek != nil && len (buf ) > 0 {
4398
4395
bek , err := genBlockEncryptionKey (mb .fs .fcfg .Cipher , mb .seed , mb .nonce )
4399
4396
if err != nil {
4400
- mb .mu .Unlock ()
4401
4397
return 0 , 0 , err
4402
4398
}
4403
4399
mb .bek = bek
4404
4400
mb .bek .XORKeyStream (buf , buf )
4405
4401
}
4406
4402
n , err := mb .writeAt (buf , 0 )
4407
4403
if err != nil {
4408
- mb .mu .Unlock ()
4409
4404
return 0 , 0 , fmt .Errorf ("failed to rewrite compressed block: %w" , err )
4410
4405
}
4411
4406
if n != len (buf ) {
4412
- mb .mu .Unlock ()
4413
4407
return 0 , 0 , fmt .Errorf ("short write (%d != %d)" , n , len (buf ))
4414
4408
}
4415
4409
mb .mfd .Truncate (int64 (len (buf )))
@@ -4422,7 +4416,6 @@ func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) {
4422
4416
mb .mfd .ReadAt (lchk [:], eof - 8 )
4423
4417
copy (mb .lchk [0 :], lchk [:])
4424
4418
} else {
4425
- mb .mu .Unlock ()
4426
4419
return 0 , 0 , fmt .Errorf ("failed to truncate msg block %d, file not open" , mb .index )
4427
4420
}
4428
4421
@@ -4436,10 +4429,8 @@ func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) {
4436
4429
// Redo per subject info for this block.
4437
4430
mb .resetPerSubjectInfo ()
4438
4431
4439
- mb .mu .Unlock ()
4440
-
4441
4432
// Load msgs again.
4442
- mb .loadMsgs ()
4433
+ mb .loadMsgsWithLock ()
4443
4434
4444
4435
return purged , bytes , nil
4445
4436
}
0 commit comments