@@ -180,7 +180,12 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
180180 }, false )
181181 return
182182 }
183- defer e .semaphore .Release (1 )
183+ shouldReleaseSema := true
184+ defer func () {
185+ if shouldReleaseSema {
186+ e .semaphore .Release (1 )
187+ }
188+ }()
184189
185190 defer UpdateForkChoiceDuration (time .Now ())
186191
@@ -602,8 +607,22 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
602607 e .logger .Info ("head updated" , logArgs ... )
603608 }
604609 }
605- if * headNumber >= startPruneFrom {
606- e .runPostForkchoiceInBackground (initialCycle )
610+ if e .fcuBackgroundPrune {
611+ shouldReleaseSema = false // pass on semaphore to background goroutine doing post fcu processing
612+ go func () {
613+ defer e .semaphore .Release (1 )
614+ err := e .runPostForkchoice (* headNumber , initialCycle )
615+ if err != nil {
616+ e .logger .Error ("run post fork choice in background" , "error" , err )
617+ }
618+ }()
619+ } else {
620+ err := e .runPostForkchoice (* headNumber , initialCycle )
621+ if err != nil {
622+ e .logger .Error ("run post fork choice in background" , "error" , err )
623+ sendForkchoiceErrorWithoutWaiting (e .logger , outcomeCh , err , stateFlushingInParallel )
624+ return
625+ }
607626 }
608627
609628 sendForkchoiceReceiptWithoutWaiting (outcomeCh , & executionproto.ForkChoiceReceipt {
@@ -613,18 +632,9 @@ func (e *EthereumExecutionModule) updateForkChoice(ctx context.Context, original
613632 }, stateFlushingInParallel )
614633}
615634
616- func (e * EthereumExecutionModule ) runPostForkchoiceInBackground (initialCycle bool ) {
617- if ! e .doingPostForkchoice .CompareAndSwap (false , true ) {
618- return
619- }
620- go func () {
621- defer e .doingPostForkchoice .Store (false )
622- var timings []interface {}
623- // Wait for semaphore to be available
624- if e .semaphore .Acquire (e .bacgroundCtx , 1 ) != nil {
625- return
626- }
627- defer e .semaphore .Release (1 )
635+ func (e * EthereumExecutionModule ) runPostForkchoice (headNumber uint64 , initialCycle bool ) error {
636+ var timings []interface {}
637+ if headNumber >= startPruneFrom {
628638 pruneStart := time .Now ()
629639 defer UpdateForkChoicePruneDuration (pruneStart )
630640 if err := e .db .Update (e .bacgroundCtx , func (tx kv.RwTx ) error {
@@ -634,16 +644,14 @@ func (e *EthereumExecutionModule) runPostForkchoiceInBackground(initialCycle boo
634644 if pruneTimings := e .executionPipeline .PrintTimings (); len (pruneTimings ) > 0 {
635645 timings = append (timings , pruneTimings ... )
636646 }
637-
638647 return nil
639648 }); err != nil {
640- e .logger .Error ("runPostForkchoiceInBackground" , "error" , err )
641- return
649+ return err
642650 }
643-
644- if len (timings ) > 0 {
645- timings = append (timings , "initialCycle" , initialCycle )
646- e .logger .Info ("Timings: Post-Forkchoice" , timings ... )
647- }
648- }()
651+ }
652+ if len (timings ) > 0 {
653+ timings = append (timings , "initialCycle" , initialCycle )
654+ e .logger .Info ("Timings: Post-Forkchoice" , timings ... )
655+ }
656+ return nil
649657}
0 commit comments