-
Notifications
You must be signed in to change notification settings - Fork 130
Fix message iteration by index and range #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Viktor Kreschenski <viktor.kreschenski@altran.com>
6822c17 to
d0404dc
Compare
tpajenkamp-dspace
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkresch Thanks for the update, now get_message_by_index works without problems.
However, there is a bug in get_messages_in_index_range if more than one message is extracted. I created a comment directly at the offending code line. Currently, I get:
google.protobuf.message.DecodeError: Truncated message.
This also affects other methods like get_messages which call get_messages_in_index_range.
Also, I noticed another possible problem in this method which is technically another issue and may be somewhat intended. When the generator returned by get_messages_in_index_range is consumed (reaches StopIteration), the underlying file is closed so that following calls to most methods cause ValueError: seek of closed file or similar errors.
format/OSITrace.py
Outdated
| for rel_index, rel_message_offset in enumerate(rel_message_offsets): | ||
| rel_begin = rel_message_offset + self._int_length | ||
| rel_end = ( | ||
| rel_message_offsets[rel_index + 1] - self._int_length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._int_length must not be subtracted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason I cannot reproduce the bug with more than one message extraction with my osi file. Can you maybe share your osi file if possible? Try to validate your osi file with osi visualizer or the osi validator. Can you visualize your file in osi visualizer?
The other issue is the generator property of python. Generators generate the elements on the fly and throw them away. A way to prevent it would be creating a deep copy of the generator as an iterator (making copies is of course performance heavy but would for now work as a quick fix).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe an error exceptions needs to be added so that the user knows whats going on. Good point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, I would argue with logic. The first message_offset is 0. Every message offset indicates the first byte of the message length indicator of its following serialized message. Thus, it is the byte right after the previous binary message (first offset excluded). The length indicator offset is already taken into account by the computation of rel_begin. By also subtracting self._int_length from the next message offset to compute rel_end, an incomplete serialized message is read missing the last 4 bytes. The next operation starts at the next message_offset entry so that these 4 bytes are skipped and never processed.
The osi-validator suffers from the same problem.
I have no appropriate means for file uploads, but this simple script generates an OSI trace file. It uses the same mechanism to pack the osi messages as txt2osi.py. The bug is triggers in the last line. If I apply my proposed fix, it works.
import struct
from osi3.osi_version_pb2 import InterfaceVersion, current_interface_version
from osi3.osi_sensorview_pb2 import SensorView
import OSITrace
filePath = "osi_SensorView_tmp.osi"
sv = SensorView()
sv.version.CopyFrom(InterfaceVersion.DESCRIPTOR.file.GetOptions().Extensions[current_interface_version])
with open(filePath, "wb") as fp:
for i in range(15):
sv.timestamp.seconds = 0
sv.timestamp.nanos = i * 5
msgBin = sv.SerializeToString()
filePath = r"_osi_SensorView_tmp.osi"
fp.write(struct.pack("<L", len(msgBin)))
fp.write(msgBin)
osiTrace = OSITrace.OSITrace()
osiTrace.from_file(filePath, type_name="SensorView")
msgs = list(osiTrace.get_messages())There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right! I checked with different files. But I don't get the "Truncated error message" (is this maybe some edge case?). When decoding there are missing fields when I keep the self._int_length subtraction happening see output below:
(I just created two readable outputs with and without the int subtractions and compared both files with meld. The right one should be the correct one!)
I will adjust it and will also add a meaningful error message when the message generator reaches the end! Thanks for reporting this issue!
Signed-off-by: Viktor Kreschenski <viktor.kreschenski@altran.com>
884a9c5 to
e8f3317
Compare
|
With the latest commit, it works for me. Thank you. |
|
@jdsika please merge. Thanks ;) |
* Replace osi message iteration * Fix int subtraction and remove file closing Signed-off-by: Viktor Kreschenski <viktor.kreschenski@altran.com> Signed-off-by: Habedank Clemens <qxs2704@europe.bmw.corp>

Reference to a related issue in the repository
For message iteration there is currently a bug in which the range cannot be specified (#426).
Add a description
What is this change?
This PR replaces hte while loop iteration with the for loop iteration over the messages inspired from the osi-validator.
What does it fix?
It fixes the index specification of OSI trace files. (Fixes #426)
How has it been tested?
I ran manual test with an osi trace file to get messages by index. I also tested the conversion from *.txt -> *.osi and made the *.osi file also readable.
Mention a member
@tpajenkamp-dspace let me know if this PR fixes your issue. Please test it with your trace file and let me know if it works.
Check the checklist