← Back to Pull requests

Support resending last message on failure #4081

Closed
valdaarhun closed this pull request without merging on Sep 24, 2025 19:51 UTC
View on GitHub
valdaarhun valdaarhun commented on Aug 27, 2025 18:48 UTC
Author

| | |
|:---|:---|
|_Related Issue(s)_| #3994 |
|_Has Unit Tests (y/n)_| |
|_Documentation Included (y/n)_| |
|_Generative AI was used in this contribution (y/n)_| |

---

Change Description

<!-- A description of the changes contained in the PR. -->
Update Svc::ComQueue to resend the last message on failure. Make this change config-driven.

Rationale

<!-- A rationale for this change. e.g. fixes bug, or most projects need XYZ feature. -->
Enable ComQueue to retry sending the last message when it receives Fw::Success::FAILURE instead of simply waiting till it receives Fw::Success::SUCCESS.

Testing/Review Recommendations

<!-- Fill in testing procedures, specific items to focus on for review, or other info to help the team verify these changes are flight-quality. -->

Future Work

<!-- Note any additional work that will be done relating to this issue. -->

  • Refine/rework implementation

  • Add tests

  • Modify docs

  • AI Usage (see policy)

    <!-- If AI was used, please describe how it was utilized (e.g., code generation, documentation, testing, debugging assistance, etc.). -->
    None

    Diff: 33 lines (+29 / -4) across 3 files Reviewer: thomas-bc Review duration: 40382m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.27) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.27 · 33 lines diff across 3 files
    0.27
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.72 · 5 substantive comment(s)
    0.72
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    83%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.211
    Real PR Conversation Timeline
    28 items
    valdaarhun valdaarhun commented on Aug 27, 2025 18:53 UTC
    Author

    Hi. I am not sure if my approach is right. I have added a config flag in FpConfig.h and have wrapped my changes in macros. The default behavior is for ComQueue to continue waiting until the next Success signal.

    thomas-bc thomas-bc commented on code on Aug 27, 2025 19:06 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    Our coding standards discourage this practice of conditionally building code with the help of the preprocessor. An alternate solution would be to use a .configure(bool does_retry) method that can set a member variable into the component as to whether it should retry or not. Then add this to your conditionals.

    valdaarhun valdaarhun commented on code on Aug 27, 2025 19:10 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    Thank you for the review. I'll check this out.

    thomas-bc thomas-bc commented on code on Aug 27, 2025 19:10 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    Once this is done it would also be good to update the UTs to test that retries functions properly.

    valdaarhun valdaarhun commented on code on Aug 27, 2025 19:11 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    Sure thing. I'll do that.

    thomas-bc thomas-bc commented on code on Aug 27, 2025 19:13 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    Awesome. Thanks a lot for contributing! 🚀
    This is a much needed feature.

    LeStarch LeStarch commented on code on Aug 27, 2025 19:16 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE

    @valdaarhun we already have a configure method, so following the suggested approach I'd recommend you add a bool arg to it e.g.

    void ComQueue::configure(..., bool retry_on_failure)
    

    LeStarch LeStarch commented on code on Aug 27, 2025 19:18 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                if (!this->m_hasRetried) {

    We technically should only send a retry when "SUCCESS" is received. When a failed message comes in, we should set a m_need_retry boolean here.

    If m_need_retry is set we should read your member variable and send there. I'll leave that note there too.

    LeStarch LeStarch commented on code on Aug 27, 2025 19:19 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();

    This should likely now read:

    ```
    if (m_need_retry) {
    this->dataOut_out(0, this->m_outBuffer, this->m_context);
    } else {
    processQueue();
    }

    LeStarch LeStarch requested changes on Aug 27, 2025 19:21 UTC
    Changes Requested Reviewer

    I agree with @thomas-bc on the awesomeness of this change. However, we need to move where sends happen.

    Thanks!

    valdaarhun valdaarhun commented on code on Aug 28, 2025 18:34 UTC
    Code Review
    Svc/ComQueue/ComQueue.cpp:1
    @@ -154,12 +159,21 @@ void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condi
             // On success, the queue should be processed. On failure, the component should still wait.
             case WAITING:
                 if (condition.e == Fw::Success::SUCCESS) {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                this->m_hasRetried = false;
    +#endif
                     this->m_state = READY;
                     this->processQueue();
                     // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
                     FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
                               static_cast<FwAssertArgType>(this->m_state));
                 } else {
    +#if FW_COM_BUFFER_RETRY_ON_FAILURE
    +                if (!this->m_hasRetried) {

    Thank you for the review. I think I misunderstood how this is supposed to work. I'll fix it.

    valdaarhun valdaarhun commented on Aug 31, 2025 18:38 UTC
    Author

    Before testing my changes, I thought I would run the unit tests in the devel branch to see what it looks like.

    I ran the following in the ComQueue directory:

    fprime-util generate --ut
    fprime-util check
    

    But it fails on the first test with an ASAN error - AddressSanitizer: stack-use-after-return.

    I am not sure if this is a known issue, or if there's an issue in my setup.

    Output:
    ----------------------------------------------------------
    [==========] Running 8 tests from 1 test suite.
    [----------] Global test environment set-up.
    [----------] 8 tests from Nominal
    [ RUN      ] Nominal.Send
    =================================================================
    ==30175==ERROR: AddressSanitizer: stack-use-after-return on address 0x7b36a8d03cf8 at pc 0x56cac320d807 bp 0x7ffefd298f50 sp 0x7ffefd298f40
    READ of size 1 at 0x7b36a8d03cf8 thread T0
        #0 0x56cac320d806 in testing::AssertionResult testing::internal::CmpHelperEQ<unsigned char, unsigned char>(char const*, char const*, unsigned char const&, unsigned char const&) ~/fprime/googletest/googletest/include/gtest/gtest.h:1358
        #1 0x56cac320a13f in testing::AssertionResult testing::internal::EqHelper::Compare<unsigned char, unsigned char, (void*)0>(char const*, char const*, unsigned char const&, unsigned char const&) ~/fprime/googletest/googletest/include/gtest/gtest.h:1377
        #2 0x56cac31df022 in Svc::ComQueueTester::emitOneAndCheck(short, unsigned char*, unsigned long) ~/fprime/Svc/ComQueue/test/ut/ComQueueTester.cpp:72
        #3 0x56cac31e0194 in Svc::ComQueueTester::testQueueSend() ~/fprime/Svc/ComQueue/test/ut/ComQueueTester.cpp:88
    ...
    Address 0x7b36a8d03cf8 is located in stack of thread T0 at offset 248 in frame
        #0 0x56cac32fb7f1 in Svc::ComQueue::processQueue() ~/fprime/Svc/ComQueue/ComQueue.cpp:285
    
      This frame has 3 object(s):
        [48, 72) 'temp' (line 328)
        [112, 184) 'buffer' (line 308)
        [224, 760) 'comBuffer' (line 304) <== Memory access at offset 248 is inside this variable
    HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
          (longjmp and C++ exceptions *are* supported)
    SUMMARY: AddressSanitizer: stack-use-after-return ~/fprime/googletest/googletest/include/gtest/gtest.h:1358 in testing::AssertionResult testing::internal::CmpHelperEQ<unsigned char, unsigned char>(char const*, char const*, unsigned char const&, unsigned char const&)
    

    The issue seems to arise here.

    LeStarch LeStarch commented on Sep 03, 2025 22:31 UTC
    Contributor

    @valdaarhun let's spin this off as a separate issue. For now, just verify the tests pass.

    LeStarch LeStarch commented on Sep 03, 2025 22:32 UTC
    Contributor

    You can run

    fprime-util generate --ut --disable-sanitizers
    

    to turn them off.

    valdaarhun valdaarhun commented on Sep 04, 2025 15:46 UTC
    Author

    You can run

    >
    > fprime-util generate --ut --disable-sanitizers
    > 

    > to turn them off.

    Thank you. I'll focus on the tests for now, and will open an issue for the failure with sanitizers enabled.

    valdaarhun valdaarhun commented on Sep 11, 2025 18:57 UTC
    Author

    While working on the tests I realized that my implementation is incorrect. Based on what I have understood, the following won't work because the assignment performs a shallow copy of the underlying data.

    this->m_outBuffer = buffer; // or outBuffer
    

    Once Fw::Buffer goes out of scope, the underlying data pointed to by m_outBuffer->m_bufferData will not exist.

    I was thinking of saving the message in a U8 array instead and wrapping it in a buffer when attempting a resend. What are your thoughts on this?

    LeStarch LeStarch commented on Sep 12, 2025 03:18 UTC
    Contributor

    First, great catch! There are two types of of data here:

    1. Fw::Buffer data. This data is owned by an external manager, and isn't invalidated until this line: https://github.com/nasa/fprime/blob/9871fd560832937172f5f96a5cc9c912f7fd785a/Svc/ComQueue/ComQueue.cpp#L206

    2. Fw::Com this data is invalidated when this buffer goes out of scoper. https://github.com/nasa/fprime/blob/9871fd560832937172f5f96a5cc9c912f7fd785a/Svc/ComQueue/ComQueue.cpp#L304 This itself may be a bug...because this happens after the call.

    I'd recommend making these changes:

    1. Moving item 2 above to be a member variable
    2. You can guarantee that item 1 happens before the comStatus return because it is sync and comStatus is async. Therefore you could set state to track the returned buffer, and then call the buffer return linked about in the comStatus handler if and only if you don't need to retry.

    this is a bit annoyingly complicated.

    LeStarch LeStarch commented on Sep 12, 2025 03:21 UTC
    Contributor

    @thomas-bc do we need comStatus anymore? Now that we have DataWithContext, could the status be in the context?

    For now we could add status to context in addition. Then only deallocate (line 206) if the status is good or if the component is "not retry".

    LeStarch LeStarch commented on Sep 22, 2025 15:51 UTC
    Contributor

    I confirmed with @thomas-bc that we should maintain comStatus for the time being. That being said, we should also specify the order of which these ports should be called.

    Since data return should be synchronous, and comStatus must be asynchronous, we should assume that data return always happens first.

    @valdaarhun are you up to continue working on this, or would you like some assistance? You've done a great job exposing issues and details, but I don't want to overwhelm you with managing all the details.

    Let me know if you want some help.

    valdaarhun valdaarhun commented on Sep 22, 2025 18:22 UTC
    Author

    @valdaarhun are you up to continue working on this, or would you like some assistance? You've done a great job exposing issues and details, but I don't want to overwhelm you with managing all the details.

    > Let me know if you want some help.

    Hi @LeStarch . Sorry for the late reply. Yes, I am still very interested in continuing this.

    There are a few things that I am a little unclear on.

    2\. You can guarantee that item 1 happens before the comStatus return because it is sync and comStatus is async.

    Since data return should be synchronous, and comStatus must be asynchronous, we should assume that data return always happens first.

    I haven't really understood the points above. Is it already guaranteed that data return will happen before comStatus return? Or does that need to be handled separately? I think my confusion stems from my lack of understanding of the following 2 ports - dataReturnIn and bufferReturnOut.

    In section 4 of the documentation, they are described as ports used for returning "ownership of buffers". I haven't really understood this part.

    LeStarch LeStarch commented on Sep 23, 2025 13:25 UTC
    Contributor

    @valdaarhun gotcha! When you send out an (Fw.Buffer) buffer from com queue, you should not touch it anymore. By sending the buffer, you've transferred ownership out to another component. Ownership is returned to you when 'dataReturnIn' is called and you may work with the data.

    For this PR you may assume 'dataReturnIn' is called before 'comStatusIn', always, as defined by the protocol between this and downstream components.

    Therefore, the following steps should be taken:

    In 'dataReturnIn_handler' simply save the returned buffer in a member variable for later.

    In 'comStatusIn_handler' do the following:
    1. If status is bad, and the stored buffet is valid then resend the stored buffer.
    2. If status is good and stored buffer is valid, and original data was a few buffer, then deallocate by sending stored buffer to 'bufferReturnOut'
    3. In all cases invalidate the stored buffer (set to empty Fw.Buffer)

    Finally, move the dequeue of the com buffer to be a member variable so the data remains in scope.

    LeStarch LeStarch commented on Sep 23, 2025 13:25 UTC
    Contributor

    I know this is complicated. Data ownership usually is. Please let me know if I can offer more explanation

    valdaarhun valdaarhun commented on Sep 23, 2025 19:40 UTC
    Author

    @valdaarhun gotcha! When you send out an (Fw.Buffer) buffer from com queue, you should not touch it anymore. By sending the buffer, you've transferred ownership out to another component. Ownership is returned to you when 'dataReturnIn' is called and you may work with the data.

    > For this PR you may assume 'dataReturnIn' is called before 'comStatusIn', always, as defined by the protocol between this and downstream components.

    Thank you for the explanation. This makes sense now.

    In 'comStatusIn_handler' do the following:

    1. If status is bad, and the stored buffet is valid then resend the stored buffer.

    2. If status is good and stored buffer is valid, and original data was a few buffer, then deallocate by sending stored buffer to 'bufferReturnOut'

    3. In all cases invalidate the stored buffer (set to empty Fw.Buffer)

    Understood. I think I am in a position to make some progress now. I'll let you know in case I get stuck again.

    LeStarch LeStarch commented on Sep 24, 2025 19:27 UTC
    Contributor

    @valdaarhun I had an insight this morning. This would work better if we just built a new "retry" component. It would import the Framer interface and implement only the retry logic.

    active component ComRetry {
            import Framer
    }
    

    Here are the main points:

    1. On dataIn send to dataOut (pass-through)
    2. On dataReturnIn store buffer and context.
    3. On comStatusIn:
    1. If good, send stored buffer and context to dataReturnOut, then call comStatusOut
    2. If bad, retry by sending stored buffer and context to dataOut

    This has the major advantage that we can put the same Retry component anywhere in the pipeline. We should therefore update the issue, and close this PR.

    Would you like to build this new component?

    LeStarch LeStarch commented on Sep 24, 2025 19:29 UTC
    Contributor

    @valdaarhun Also, I apologize for all the back and forth. I was building another com component yesterday and thought "I need the same retry logic here as in comQueu" then I realized it would stand better as a separate component.

    We do really appreciate your work on this issue. It has exposed many insights into this process that are refining and making the code better. We are learning on this end too.

    valdaarhun valdaarhun commented on Sep 24, 2025 19:35 UTC
    Author

    Hi @LeStarch . I agree. I think this approach would be better especially if it can be made extensible so other components can make use of it too.

    I would love to work on building this new component.

    LeStarch LeStarch commented on Sep 24, 2025 19:51 UTC
    Contributor

    @valdaarhun thanks for your understanding. I will close this PR. Please feel free to make a PR with this new component. We can then review together!

    Reviewers
    Assignees
    valdaarhun valdaarhun
    Labels
    risk: low confidence: 83.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 27% (LOW) · Higher = larger blast radius
    27%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 83.8% · Lower = rushed or shallow review
    83.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.4% · Safe to merge
    4.4%
    Repository