Behavior of OpenCV FileStorage APPEND mode during retry after transient I/O failure
0 reputation · 24 Aug 2021, 05:54 UTC
0 reputation · 24 Aug 2021, 05:54 UTC
Goal: Use OpenCV’s FileStorage with the APPEND flag to safely retry a write operation after a temporary I/O error without duplicating previously written nodes.
Constraints: The application must detect a failed << operation or open() call, decide whether to reopen the storage in APPEND mode, and rely on the flag to position the file pointer at the end of the existing XML/YAML file. Uncertainty remains about whether a retry after a partial write could leave an incomplete node that causes duplication or malformed content on subsequent attempts, and whether the APPEND flag alone guarantees idempotent retries when the same logical data is written again.
Specific questions:
FileStorage in APPEND mode after a failed write guarantee that the file pointer is correctly positioned at the end of the last successfully written node, even if the failure occurred mid‑node?APPEND produce duplicate nodes, and how should applications avoid or detect such duplication?APPEND-based retry logic?32775 reputation · 24 Aug 2021, 17:16 UTC
Reopening a FileStorage object with the APPEND flag after a transient I/O failure positions the write pointer at the end of the file, provided the file itself is intact and the underlying open() call succeeds.
APPEND mode OpenCV opens the file with std::ios::app; each write operation seeks to the current end‑of‑file before serializing a node.failbit is set. Subsequent << operations are ignored until the error state is cleared.FileStorage object does not automatically clear the error state; you must check isOpened() or the stream’s error flags, release the storage, and reopen the same file with the APPEND flag to reset the stream.When the failure occurs mid‑node, the partially written bytes remain in the file, but the stream’s error state prevents further writes. Reopening with APPEND does not truncate or repair the file; it simply positions the pointer at the current end‑of‑file (which includes the partial node). Therefore, the retry will add new data after the incomplete node, leaving the file with a malformed segment unless the application detects and discards the partial node.
operator<< (or write()) and test storage.fail().storage.release() to close the stream and clear the internal FileStorage state.FileStorage storage(filename, FileStorage::APPEND).storage.isOpened() is true and !storage.fail() before proceeding.Can you confirm whether the file contains a partially written node after the failure? If a partial node is present, the application must either truncate the file to the last good boundary or detect and skip the malformed content on retry; otherwise simply reopening in APPEND mode will leave corrupted data in the file.
// Simulate transient failure
cv::FileStorage fs("test.yml", cv::FileStorage::WRITE | cv::FileStorage::APPEND);
fs << "mat" << cv::Mat::eye(3,3,CV_64F);
// Inject error: close underlying file descriptor or simulate disk full
// ...
if (fs.fail()) {
fs.release();
fs.open("test.yml", cv::FileStorage::APPEND); // retry
fs << "mat" << cv::Mat::eye(3,3,CV_64F);
}
fs.release();
// Read back and check for duplicates or corruption
cv::FileStorage fr("test.yml", cv::FileStorage::READ);
// ...
Use comments to ask for clarification. Post a solution as an answer.
32,775 reputation · 24 Aug 2021, 13:37 UTC
To build on the previous point regarding malformed segments, it is important to clarify that FileStorage::APPEND operates at the file-system level rather than the semantic level. Because OpenCV does not implement transactional writes, a failure mid-node leaves a physical fragment of XML/YAML data at the end of the file.
When you reopen the file in APPEND mode (assuming OpenCV 4.x), the pointer is placed at the absolute end of the file. This leads to two critical risks during a retry:
For production-grade retries, verify the file integrity or use a temporary sidecar file to ensure the node is fully committed before appending it to the primary storage.