Baileys
Baileys copied to clipboard
[BUG] creds.json broke after some reconnections.
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:
- Created a new connection
- 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
connectOptionslook 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
+1
+1
+1
help me
👍🏽 Upvote! This issue deserves attention.
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
up+
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.
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?
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.
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.
help me🙂
Nice
😯
Same here!!!!
Up +1
up Same here
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; } } }
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
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" ?
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.
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
+1
+1
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 {}
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 https://github.com/nodejs/node/issues/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
you most likely have killed the process when it was still writing the file.
@SheIITear or it's because
fs/promises.writeFilehas been used - see the similar issue in nodejs nodejs/node#26338The file system (and nodejs) doesn't care about concurrent writes, and because on
creds.updateevent we usually just callsaveCreds(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
useMultiFileAuthStateso we can add someConcurrent(saveCreds)wrapper or even smthg likeDebounce(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
Here you're! @SheIITear have a look when you have time :pray: https://github.com/WhiskeySockets/Baileys/pull/824
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!
The disconnect problem started after this change: https://github.com/WhiskeySockets/Baileys/commit/fbbb511fb8464ea744e2abc416c8cef54c0e317c