Support resending last message on failure #4081
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.
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.
Thank you for the review. I'll check this out.
Once this is done it would also be good to update the UTs to test that retries functions properly.
Sure thing. I'll do that.
Awesome. Thanks a lot for contributing! 🚀
This is a much needed feature.
@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)
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.
This should likely now read:
```
if (m_need_retry) {
this->dataOut_out(0, this->m_outBuffer, this->m_context);
} else {
processQueue();
}
I agree with @thomas-bc on the awesomeness of this change. However, we need to move where sends happen.
Thanks!
Thank you for the review. I think I misunderstood how this is supposed to work. I'll fix it.
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.
@valdaarhun let's spin this off as a separate issue. For now, just verify the tests pass.
You can run
fprime-util generate --ut --disable-sanitizers
to turn them off.
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.
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?
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.
@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".
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 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 issyncandcomStatusis 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.
@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.
I know this is complicated. Data ownership usually is. Please let me know if I can offer more explanation
@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.
@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?
@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.
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.
@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!
thomas-bc
valdaarhun
| | |
|:---|:---|
|_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::FAILUREinstead of simply waiting till it receivesFw::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. -->
AI Usage (see policy)
<!-- If AI was used, please describe how it was utilized (e.g., code generation, documentation, testing, debugging assistance, etc.). -->
None