Skip to content
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

When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader #1529

Open
jazzman55 opened this issue Aug 7, 2024 · 5 comments

Comments

@jazzman55
Copy link
Contributor

Library/API/IoT binding

System.IO.FileSystem, System.IO.Streams

Visual Studio version

VS2022

.NET nanoFramework extension version

No response

Target name(s)

ESP32_REV3

Firmware version

1.12.0.32

Device capabilities

No response

Description

Reading text file with StreamReader, the underlying FileStream resets its position to 0 every time StreamReader's buffer is filled up. This causes endless jumps to beginning of the file and thus the file can't be read to end.

How to reproduce

  1. Upload text file to ESP32 config partition. The file must be longer than StreamReader's buffer size (512B).
  2. Open the file and read it line-by-line with StreamReader.ReadLine().

Expected behaviour

File is correctly read line-by-line from start to end. When end of file is reached ReadLine() returns null.

Screenshots

image

Sample project or code

//The last line of loremipsum.txt is <END>
        [TestMethod]
        public void ReadText_StreamReader()
        {
            Debug.WriteLine(nameof(ReadText_StreamReader));
            string line;
            string text = "";
            using (var fs = new StreamReader(new FileStream(@"I:\loremipsum.txt", FileMode.Open, FileAccess.Read)))
            {
                line = fs.ReadLine();
                int i = 0;
                while (line != null && i++ < 50) //exit after reading 50 lines to avoid endless loop
                {
                    Debug.WriteLine(line);
            
                    line = fs.ReadLine();
                    if (line != null)
                        text = line;
                }
            }

            Assert.Equal("<END>", text);
        }
[loremipsum.txt](https://github.com/user-attachments/files/16523448/loremipsum.txt)

Flash to ESP32 with:

@echo off
set port=COM5
littlefs-python create ..\FileSystem %tmp%\config_image.bin --fs-size=262144 --block-size=4096
python %idf_path%\components\partition_table\parttool.py -p %port% write_partition -n config --input %tmp%\config_image.bin 
del %tmp%\config_image.bin 

Aditional information

Workaround:
Track stream's position in a separate variable and set the stream position before each read:
https://github.com/CShark-Hub/Mako-IoT.Device.Services.FileStorage/blob/main/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs#L525

Note:
This was working OK with one of the previous versions of System.IO.FileSystem

@nfbot nfbot changed the title When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader. When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader Aug 7, 2024
@jazzman55
Copy link
Contributor Author

The test file
loremipsum.txt

@josesimoes
Copy link
Member

The unit tests must not be covering this either because the content isn't large enough or the code there is missing this operation... 🤔

Anyway, not surprising that it had a different behavior before, as the Stream has been introduced only recently.

@Akshit018
Copy link

Akshit018 commented Aug 12, 2024

Subject: Request to Work on Good First Issue

Hi @jazzman55 ,

I noticed the issue with StreamReader and FileStream and believe it would be a great first issue for me to work on. I'm eager to contribute and would love the opportunity to tackle this.

May I take it on?

Thank you,
Akshit

@josesimoes
Copy link
Member

Hey @Akshit018!
That's great that you're willing to tackle this one. 🥳
I'm assigning it to you now.

Please take a look at the contribution guide here. This will typically require a PR to the Sys.IO.FileSystem repo.
We 💜 PRs so feel free to start one as early as you want so you can have early feedback.

If you have questions or want to discuss anything, suggest you head over to our Discord server and engage a conversation there.

@josesimoes
Copy link
Member

@Akshit018 curious if you had the time to start working on this one? 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants