Baileys icon indicating copy to clipboard operation
Baileys copied to clipboard

[BUG] creds.json broke after some reconnections.

Open ElielsonU opened this issue 1 year ago • 18 comments

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 avatar May 16 '24 17:05 ElielsonU

+1

jonhy751 avatar May 16 '24 18:05 jonhy751

+1

luis-luciqm avatar May 16 '24 18:05 luis-luciqm

+1

caioRafael avatar May 16 '24 18:05 caioRafael

help me

FabioRocha462 avatar May 16 '24 18:05 FabioRocha462

👍🏽 Upvote! This issue deserves attention.

Mirlaaa avatar May 16 '24 18:05 Mirlaaa

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

Alien-Alfa avatar May 17 '24 01:05 Alien-Alfa

up+

MaznAbdullah avatar May 17 '24 08:05 MaznAbdullah

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.

ElielsonU avatar May 17 '24 10:05 ElielsonU

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?

SheIITear avatar May 17 '24 14:05 SheIITear

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.

ElielsonU avatar May 17 '24 16:05 ElielsonU

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.

SheIITear avatar May 17 '24 20:05 SheIITear

help me🙂

KazeDevID avatar May 20 '24 23:05 KazeDevID

Nice

Im-Dims avatar May 21 '24 02:05 Im-Dims

😯

iqstore78 avatar May 21 '24 06:05 iqstore78

Same here!!!!

davidlg avatar May 21 '24 16:05 davidlg

Up +1

Salientekill avatar May 22 '24 00:05 Salientekill

up Same here

raphaelvserafim avatar May 22 '24 18:05 raphaelvserafim

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; } } }

raphaelvserafim avatar May 22 '24 19:05 raphaelvserafim

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

Alien-Alfa avatar May 23 '24 05:05 Alien-Alfa

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" ?

punitagarwal4 avatar May 23 '24 09:05 punitagarwal4

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.

raphaelvserafim avatar May 23 '24 13:05 raphaelvserafim

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

punitagarwal4 avatar May 24 '24 07:05 punitagarwal4

+1

lucianodltec avatar May 24 '24 14:05 lucianodltec

+1

allanlisboa avatar May 24 '24 18:05 allanlisboa

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 avatar May 27 '24 03:05 allburov

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

allburov avatar May 27 '24 04:05 allburov

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

Salientekill avatar May 27 '24 05:05 Salientekill

Here you're! @SheIITear have a look when you have time :pray: https://github.com/WhiskeySockets/Baileys/pull/824

allburov avatar May 27 '24 06:05 allburov

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!

natanjborba avatar May 27 '24 18:05 natanjborba

The disconnect problem started after this change: https://github.com/WhiskeySockets/Baileys/commit/fbbb511fb8464ea744e2abc416c8cef54c0e317c

Salientekill avatar May 29 '24 21:05 Salientekill