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

[BUG] creds.json broke after some reconnections. #794

Closed
ElielsonU opened this issue May 16, 2024 · 35 comments · Fixed by #824
Closed

[BUG] creds.json broke after some reconnections. #794

ElielsonU opened this issue May 16, 2024 · 35 comments · Fixed by #824
Labels
bug Something isn't working

Comments

@ElielsonU
Copy link

ElielsonU commented May 16, 2024

Describe the bug
Actually, i've been ignoring status codes diferent than 401 and reconecting the session. But after some reconnections the creds.json broke and the session is lost. But funny, when you fix the json the connection normalize again.

To Reproduce
Steps to reproduce the behavior:

  1. Created a new connection
  2. Reconnect the session ignoring status codes diferent than 401

Expected behavior
It was expected to maintan the session running.

Environment (please complete the following information):

  • Is this on a server? in pcs and servers too
  • What do your connectOptions look like?
    ´´´
    {
    printQRInTerminal: true,
    auth: state,
    keepAliveIntervalMs: 999999,
    browser: Browsers.macOS("Desktop"),
    syncFullHistory: false,
    logger: pino({ level: "fatal" }),
    linkPreviewImageThumbnailWidth: 852,
    generateHighQualityLinkPreview: true,
    options: {
    timeout: 15 * 1000,
    },
    }
    ´´´
  • Do you have multiple clients on the same IP? Yes
  • Are you using a proxy? No

image

@ElielsonU ElielsonU added the bug Something isn't working label May 16, 2024
@jonhy751
Copy link

+1

2 similar comments
@luis-luciqm
Copy link

+1

@caioRafael

This comment has been minimized.

@FabioRocha462
Copy link

help me

@Mirlaaa
Copy link

Mirlaaa commented May 16, 2024

👍🏽 Upvote! This issue deserves attention.

@Alien-Alfa
Copy link

Good one! this is a serious issue.
I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart.
This is automated but the root of the problem is not fixed

@MaznAbdullah

This comment has been minimized.

@ElielsonU
Copy link
Author

Good one! this is a serious issue. I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart. This is automated but the root of the problem is not fixed

Nice! After all, i've tested some ways to fix this. And you can fix the broken json by erasing all the characters after the penutimate brace. With the json fixed, you can just reconnect and all will work fine. You don't need to read the qr_code again.

@SheIITear
Copy link
Collaborator

Good one! this is a serious issue. I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart. This is automated but the root of the problem is not fixed

Nice! After all, i've tested some ways to fix this. And you can fix the broken json by erasing all the characters after the penutimate brace. With the json fixed, you can just reconnect and all will work fine. You don't need to read the qr_code again.

This won't always fix broken json as there might still be missing closing brackets and such so I wouldn't rely on that. Are you guys closing the whole process using something like sigkill or just normally having it reconnect during disconnects?

@ElielsonU
Copy link
Author

Good one! this is a serious issue. I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart. This is automated but the root of the problem is not fixed

Nice! After all, i've tested some ways to fix this. And you can fix the broken json by erasing all the characters after the penutimate brace. With the json fixed, you can just reconnect and all will work fine. You don't need to read the qr_code again.

This won't always fix broken json as there might still be missing closing brackets and such so I wouldn't rely on that. Are you guys closing the whole process using something like sigkill or just normally having it reconnect during disconnects?

It occurs in both cases. After a few times making a new connection ignoring the another error statuses or just killing the process with ctrl+c.

@SheIITear
Copy link
Collaborator

Good one! this is a serious issue. I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart. This is automated but the root of the problem is not fixed

Nice! After all, i've tested some ways to fix this. And you can fix the broken json by erasing all the characters after the penutimate brace. With the json fixed, you can just reconnect and all will work fine. You don't need to read the qr_code again.

This won't always fix broken json as there might still be missing closing brackets and such so I wouldn't rely on that. Are you guys closing the whole process using something like sigkill or just normally having it reconnect during disconnects?

It occurs in both cases. After a few times making a new connection ignoring the another error statuses or just killing the process with ctrl+c.

Force killing a process is not a good idea, you most likely have killed the process when it was still writing the file.

@KazeDevID

This comment has been minimized.

@Im-Dims

This comment has been minimized.

@iqstore78

This comment has been minimized.

@davidlg

This comment has been minimized.

@Salientekill

This comment has been minimized.

@raphaelvserafim

This comment has been minimized.

@raphaelvserafim
Copy link

raphaelvserafim commented May 22, 2024

My solution for now is this

export async function fixJSON(filePath: string) { const jsonString = await fsPromises.readFile(filePath, "utf8"); try { return JSON.parse(jsonString); } catch (error) { if (error instanceof SyntaxError) { try { var fixedString = jsonString.replace(/}"(lastPropHash)/g, ',"$1'); await fsPromises.writeFile(filePath, fixedString, "utf8"); } catch (innerError) { return true; } } else { return true; } } }

@Alien-Alfa
Copy link

Good one! this is a serious issue. I had to make adjustments in my code to fix this like: when creds.json break it will show qr so if(s.qr) Delete the current session; pull the session from the saved server; restart. This is automated but the root of the problem is not fixed

Nice! After all, i've tested some ways to fix this. And you can fix the broken json by erasing all the characters after the penutimate brace. With the json fixed, you can just reconnect and all will work fine. You don't need to read the qr_code again.

Actually my creds.json is saved in the cloud so when the creds in bot breaks it will show the qr if that happens my function will replace the creds with the working file from cloud.

or you can just save a backup creds.json file in the bot itselff and do as I did
if(error) replace the session/creds.json with backup/creds.json

@punitagarwal4
Copy link

My solution for now is this

export async function fixJSON(filePath: string) { const jsonString = await fsPromises.readFile(filePath, "utf8"); try { return JSON.parse(jsonString); } catch (error) { if (error instanceof SyntaxError) { try { var fixedString = jsonString.replace(/}"(lastPropHash)/g, ',"$1'); await fsPromises.writeFile(filePath, fixedString, "utf8"); } catch (innerError) { return true; } } else { return true; } } }

So when do we exactly need to call this function, when the connection state is "open" ?

@raphaelvserafim
Copy link

My solution for now is this
export async function fixJSON(filePath: string) { const jsonString = await fsPromises.readFile(filePath, "utf8"); try { return JSON.parse(jsonString); } catch (error) { if (error instanceof SyntaxError) { try { var fixedString = jsonString.replace(/}"(lastPropHash)/g, ',"$1'); await fsPromises.writeFile(filePath, fixedString, "utf8"); } catch (innerError) { return true; } } else { return true; } } }

So when do we exactly need to call this function, when the connection state is "open" ?

I have a loop that keeps checking every 6 seconds, verifying the file, because there are no events.

@punitagarwal4
Copy link

My solution for now is this
export async function fixJSON(filePath: string) { const jsonString = await fsPromises.readFile(filePath, "utf8"); try { return JSON.parse(jsonString); } catch (error) { if (error instanceof SyntaxError) { try { var fixedString = jsonString.replace(/}"(lastPropHash)/g, ',"$1'); await fsPromises.writeFile(filePath, fixedString, "utf8"); } catch (innerError) { return true; } } else { return true; } } }

So when do we exactly need to call this function, when the connection state is "open" ?

I have a loop that keeps checking every 6 seconds, verifying the file, because there are no events.

Didn't work

@lucianodltec

This comment has been minimized.

1 similar comment
@allanlisboa

This comment has been minimized.

@allburov
Copy link
Contributor

allburov commented May 27, 2024

Also experiencing the issue

a.replace(/}"(lastPropHash)/g, ',"$1')

that one helped
UPD - not always tho, because the last key can be different one, had to write "safe json loader" with stack to keep the right order for {}

@allburov
Copy link
Contributor

allburov commented May 27, 2024

you most likely have killed the process when it was still writing the file.

@SheIITear or it's because fs/promises.writeFile has been used - see the similar issue in nodejs nodejs/node#26338

The file system (and nodejs) doesn't care about concurrent writes, and because on creds.update event we usually just call saveCreds (as in the example https://github.com/WhiskeySockets/Baileys/blob/master/Example/example.ts#L215-L217) it causes race condition issues.

I'll look at useMultiFileAuthState so we can add some Concurrent(saveCreds) wrapper or even smthg like Debounce(saveCreds) alternative

@Salientekill
Copy link

you most likely have killed the process when it was still writing the file.

@SheIITear or it's because fs/promises.writeFile has been used - see the similar issue in nodejs nodejs/node#26338

The file system (and nodejs) doesn't care about concurrent writes, and because on creds.update event we usually just call saveCreds (as in the example https://github.com/WhiskeySockets/Baileys/blob/master/Example/example.ts#L215-L217) it causes race condition issues.

I'll look at useMultiFileAuthState so we can add some Concurrent(saveCreds) wrapper or even smthg like Debounce(saveCreds) alternative

If you can, it will help a lot, I'm not using the latest versions precisely because of this problem, I'm using 6.6.1

allburov added a commit to allburov/Baileys that referenced this issue May 27, 2024
allburov added a commit to allburov/Baileys that referenced this issue May 27, 2024
allburov added a commit to allburov/Baileys that referenced this issue May 27, 2024
allburov added a commit to allburov/Baileys that referenced this issue May 27, 2024
@allburov
Copy link
Contributor

Here you're!
@SheIITear have a look when you have time 🙏
#824

@natanjborba
Copy link

Here you're! @SheIITear have a look when you have time 🙏 #824

Here I was having this problem and testing your PR solved the problem, thank you!

@Salientekill
Copy link

The disconnect problem started after this change:
fbbb511

allburov added a commit to allburov/Baileys that referenced this issue May 30, 2024
@PurpShell
Copy link
Collaborator

keepAliveIntervalMs: 999999,

@PurpShell
Copy link
Collaborator

How about we buffer this event?

@azudindaem
Copy link
Contributor

How about we buffer this event?

event better.. it might affect memory, but it will prevent the JSON from breaking

@allburov
Copy link
Contributor

How about we buffer this event?

Make sense, it'll help in the file storage and any remote storages to reduce number of creds.update events!

I've checked the creds.update events we emit right now and not 100% sure how to buffer it right in a single place...
as a quick fix just for "file auth storage" I think we could live with locks forawhile, just to fix the issue asap - #824
The node-cache manager that works with files (https://github.com/rolandstarke/node-cache-manager-fs-hash) also has lock mechanism.
It doesn't affect the rest of the code, so later when we add event buffering (I'll check that) it'll work together.
WDYT @PurpShell ?

PurpShell pushed a commit that referenced this issue Jun 1, 2024
@allburov
Copy link
Contributor

allburov commented Jun 1, 2024

The fix is available in the latest github version (not in npm yet). Remember to use provided useMultiFileAuthState, if you're using your custom auth file store - update won't help!

PurpShell added a commit that referenced this issue Jun 2, 2024
* fix(master): linting

* fix(master): linting warnings

* Make "logger" optional (#772)

* fix: downloadMediaMessage return type matched parameter (#757)

* fix: downloadMediaMessage return type matched parameter

* chore: keyword-spacing eslint rule warn instead of error

* chore: linting

aaaaaaaaaaaaaaaaaaaa

* feat(add-routing-info): initial commit (#773)

* feat(add-logger): initial commit (#777)

* Remove predefinedLabels (#770)

* Remove predefinedLabels

If the user doesn't delete the label from the WA business, it's not a problem.

but it will be a problem when the user deletes the default label, it remains in store but doesn't exist in WA

* fix linting

* fix trailing

* Update chats.ts

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>

* fix shouldIgnoreJid not receiving app state keys (#792)

* fix(master): linting and making functionality better

* Update messages-recv.ts

* Update baileys-version.json

* Fix linter != to !== (#834)

* Use message-receipt.update for status@broadcast events (#833)

* Lock file on save and read (#824)

Fix #794

* feat: add event that handles join approval requests (#802)

* initial commit

* lint

* add type in method

* add more actions / fixes participant jid / rename event

* fixes

* more fixes

* fix typing

* change 'reject' to 'rejected'

* chore:linting

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>

* chore(release): v6.7.3

---------

Co-authored-by: allburov <allburov@gmail.com>
Co-authored-by: arthur simas <arthursimas1@gmail.com>
Co-authored-by: azudin <azudin.daem@gmail.com>
Co-authored-by: vini <91087061+vinikjkkj@users.noreply.github.com>
Co-authored-by: ShellTear <shelltear1337@gmail.com>
Co-authored-by: Wender Teixeira <unktools@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.