Where did I not close the stream?

there is a code

  1. When the chatlist.json does not exist, it is created and an empty list is placed in it

  2. When the chatslist.json exists, it is deserialized

     private async Task<ChatsInfo> ReadChatsList()
     {
         FileInfo file = new FileInfo(chatsListPath);
         if (file.Exists)
         {
             using (FileStream fs = new FileStream(chatsListPath, FileMode.Open))
             {
                 ChatsInfo chatsList = await JsonSerializer.DeserializeAsync<ChatsInfo>(fs);
                 fs.Close();
                 return chatsList;
             }
         }
         else
         {
             ChatsInfo emptylist = new ChatsInfo();
             emptylist.activedChats = new List<long>();
             using (FileStream fs = new FileStream(chatsListPath, FileMode.Create))
             {
                 await JsonSerializer.SerializeAsync<ChatsInfo>(fs, emptylist, new JsonSerializerOptions
                 {
                     WriteIndented = true
                 });
                 fs.Close();
             }
             return emptylist;
         }
     private async void SerializeChatsList(ChatsInfo data)
     {
         using (FileStream fs = new FileStream(chatsListPath, FileMode.Open))
         {
             await JsonSerializer.SerializeAsync<ChatsInfo>(fs, data, new JsonSerializerOptions
             {
                 WriteIndented = true
             });
             fs.Close();
         }
     }
     private async Task<bool> ChatInTheList(long chat_id)
     {
         ChatsInfo chatsList = await ReadChatsList();
         return chatsList.activedChats.Contains(chat_id);
     }
    

There is a condition

        if(!await ChatInTheList(message.Chat.Id))
        {
            AddChatInList(message.Chat.Id);
        }

An error occurs, at which the error appears

System.IO.IOException: "The process cannot access the file
'C:\Users\User\source\repos\tgbot\bin\linux-x64\netcoreapp3.1\chatslist.json'
because it is being used by another process."

Initially, this exception was created in this call stack:
    [External code]
    tgbot.Bot.ReadChatsList() в Bot.cs
    [External code]
    tgbot.Bot.ChatInTheList(long) в Bot.cs
    [External code]
    tgbot.Bot.OnMessageReceived(object, Telegram.Bot.Args.MessageEventArgs) в Bot.cs

Why doesn’t one of the threads close? Where did I go wrong?

@PanagiotisKanavos

    private async void AddChatInList(long chat_id)
    {
        ChatsInfo chatsList = await ReadChatsList();
        chatsList.activedChats.Add(chat_id);
        SerializeChatsList(chatsList);

    }

>Solution :

You don’t await AddChatInList(), so while that method is running, another method is trying to access the file.

Also, do you want to thrash your SSD? Because this is how you’ll thrash your SSD. (No, not really, those things can write a lot of data, but…) every chat line results in a read-write operation of the entire file, which will grow with each chat message. Your system will become unbearably slow.

Leave a Reply