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

Invalid JSON in FileCookieJar #1883

Closed
greg0ire opened this issue Jul 26, 2017 · 5 comments
Closed

Invalid JSON in FileCookieJar #1883

greg0ire opened this issue Jul 26, 2017 · 5 comments

Comments

@greg0ire
Copy link
Contributor

greg0ire commented Jul 26, 2017

Q A
Bug? yes
New Feature? no
Version 5.3.1

Actual Behavior

Invalid json is saved in the file used as backend for FileCookieJar. I have had this issue in the past with what might be another version, I remember there were 2 extra brackets at the end of the file. I experienced the issue today again, but I arrived too late to see the invalid contents, which had already been replaced (which kind of makes me think that there might have been many concurrent writes). I know it was invalid because I got: Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON and it came from here.

Expected Behavior

A cookie jar containing valid JSON

Steps to Reproduce

I'm not sure how to reproduce this, I think it might happen with many concurrent writes.

Possible Solutions

Just prevent concurrent writes:

-        if (false === file_put_contents($filename, json_encode($json))) {
+        if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) {
greg0ire added a commit to greg0ire/guzzle that referenced this issue Jul 26, 2017
Concurrent writes might lead to invalid JSON being saved in the cookie jar.
Hopefully fixes guzzle#1883 .
@jbilbo
Copy link

jbilbo commented May 24, 2019

This exact same thing happened to me too. Also the extra ] at the end.
@greg0ire did your patch fix it for you?

@greg0ire
Copy link
Contributor Author

I'm not sure I tried it on a production system, maybe you can try it?

@jbilbo
Copy link

jbilbo commented May 24, 2019

I'm monitoring if it happens again (only happened once so far) and will try it if that's the case.

@sagikazarmark
Copy link
Member

I think #2335 should fix this issue.

@greg0ire
Copy link
Contributor Author

It's a backport of #1884 that I wrote a long time ago, so I think the issue is fixed. Let's close this and see if people reopen.

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

No branches or pull requests

3 participants