Repository navigation
Two identical parse requests can return different results #54
Description
Guys, I found one really weird big in bblfsh. I think it is related to daemon itself but cannot be 100% sure. So do not hesitate to move this issue if I am wrong.
Context
Machine learning team really want to have reproducible results everywhere where it is possible.
So we fix all random seeds to have a deterministic code. Suddenly we find out that two identical runs of style-analyzer quality estimation give different results: src-d/style-analyzer#557. Clues lead to bblfsh and I was able to reproduce the bug without our code. Let me show you.
How to reproduce
Run in bash
docker run -d --rm --name style_analyzer_bblfshd --privileged -p 9432\:9432 \
bblfsh/bblfshd\:v2.11.0 --log-level DEBUG
docker exec style_analyzer_bblfshd bblfshctl driver install \
javascript docker://bblfsh/javascript-driver\:v2.6.0
# clone repo with data
git clone https://github.com/laravel/telescope
git checkout 6f0a10ec586cfa1a22218b6778bf9c1572b97912Also checked for v1.2.0 driver version.
Run in python3
from collections import defaultdict
import pickle
import time
import os
import bblfsh
bc = bblfsh.BblfshClient("localhost:9432")
res = defaultdict(lambda: defaultdict(int))
times = defaultdict(list)
iter_n = 10
for i in range(iter_n):
for (dirpath, dirnames, filenames) in os.walk("telescope"):
for filename in filenames:
try:
t_start = time.time()
filepath = os.path.join(dirpath, filename)
if ".git" in filepath:
continue
resp = bc.parse(filename=filepath)
if len(res[filepath]) > 0 and resp.status not in res[filepath]:
print("iter %.3d New status %d for filepath %s. counter: %s " % (
i + 1, resp.status, filepath, dict(res[filepath])))
res[filepath][resp.status] += 1
times[filepath].append(time.time() - t_start)
except Exception as e:
print(filepath, e)
res[filepath][repr(e)] += 1I do not think it is important but bblfsh package version is 2.12.6.
My output head
telescope/resources/.DS_Store Content must be UTF-8, ASCII or Base64 encoded
telescope/resources/.DS_Store Content must be UTF-8, ASCII or Base64 encoded
iter 003 New status 2 for filepath telescope/webpack.mix.js. counter: {0: 2}
telescope/resources/.DS_Store Content must be UTF-8, ASCII or Base64 encoded
telescope/resources/.DS_Store Content must be UTF-8, ASCII or Base64 encoded
iter 004 New status 2 for filepath telescope/resources/js/routes.js. counter: {0: 3}
iter 004 New status 2 for filepath telescope/resources/js/base.js. counter: {0: 3}
How to interpret
Mainly, if you see the message, that some file gets a new status that means that during the identical requests bblfsh return different statuses. For example, for telescope/webpack.mix.js and telescope/resources/js/routes.js it was able to parse file first but not the second time.
Notes
- If you set
iter_nto bigger number you get much more output messages. - Sometimes performance for big
iter_nbecome super bad. Can be related to perf: bblfshd 2.9 non responsive after parsing 1000+ files bblfshd#226 - If you feed only js files it works great and always parse them for
iter_n=100.
So it is important for ML team to have reproducible output from bblfsh.
Please tell me if you are able to reproduce the issue or need more info from my side.
Activity
creachadair commentedon Jan 30, 2019
Out of curiosity, have you seen this happen with languages other than JS? I'm asking since it's not yet clear whether this points to a driver bug (which I suspect) or an SDK bug (which is also possible).
zurk commentedon Jan 30, 2019
No, but it is fast to check, so I did it. Thanks for the question because I found a bug in my code snippet and fix it (first msg updated). But the bug is still on place.
So, I run the test for latest python driver and https://github.com/src-d/style-analyzer repo and it looks fine. The bblfsh output was the same during 28 iterantions and then I stop it.
I think we can safely move this one to https://github.com/bblfsh/javascript-driver/issues
creachadair commentedon Jan 30, 2019
I agree, though I don't seem to have the permissions to do that.
zurk commentedon Feb 5, 2019
@smola can I kindly ask you to move this issue to https://github.com/bblfsh/javascript-driver?
[-]Two identical bblfsh parse requests can give different results[/-][+]Two identical parse requests can return different results[/+]bzz commentedon Feb 12, 2019
Was trying to reproduce this with latest Golang v3 client instead of old python v1, to check if this is fixed in latest version using the code from details
and it is not reproduced exactly, but out of 112, there was only a single file
telescope/public/app.jswhich fails and seem to produce consistent but somehow un-deterministic error messages (different order and number of failing checks by type)First run
Another run
Other then that, there is no difference on 10 iterations using latest UASTv2-enabled client.
Details
Next step would be to confirm if this behavior of the latest client version is consistent with new Python client bblfsh/python-client#128
BTW, the offender here is this 2.4mb app.js file and same behavior (randomized errors) is easily reproducible with multiple subsequent runs of
bblfsh-cli telescope/public/app.jsdennwc commentedon Feb 12, 2019
@bzz This helps a lot, actually. The output of a failing transformation is non-deterministic (random map iteration). But it is deterministic for a successful one.
Can you please open an issue with the file that generates those errors? Thanks!
creachadair commentedon Feb 12, 2019
I suspect a map-traversal order issue on the error path.
dennwc commentedon Feb 12, 2019
@creachadair Exactly. Although I'm not sure if we want to change it - it will affect performance.
bzz commentedon Feb 12, 2019
I have updated the #54 (comment) above with the link - do you mean we need a separate issue in SDK as well?
Am planing to submit a workaround by sorting them first.
dennwc commentedon Feb 12, 2019
@bzz It's JS-related issue since it fails to transform a valid JS AST.
Regarding sorting errors, I'm not against it, but it won't fix the actual issue with the non-deterministic AST output. This is because AST traversal order is non-deterministic in DSL, and it may terminate the transform on fatal errors (read: randomly depending on the AST branch taken).
The random order is intentional since it provides better performance (no allocation or sorting) and provides some level of protection against invalid transforms that rely on iteration order. And it doesn't affect the successful transformation passes.
bzz commentedon Feb 12, 2019
makes sense, thank you for explaining! And TBH I do not think it's an issue at all - as soon parsing consistently fails for the same file, it's not a big deal having a slightly different error messages.
Ok, will just investigate further why it fails for this particular file, if it's consistent between clients and skip the sorting.
And examples of code in here might be useful for future regression testing automation that we can run on every release.
creachadair commentedon Feb 12, 2019
I don't think performance on an error path is a concern: When we get an error, we're already committed to aborting a fairly expensive transaction, so it won't happen in a loop.
In contrast, having to do subtle workarounds to compensate for time-varying output in the "bad" cases is almost certainly not worth it. Reproducibility is really really helpful. :)
zurk commentedon Feb 12, 2019
Completely agree with @creachadair
as a bblfsh user, I'd like to have reproducibility even if it affects performance.
dennwc commentedon Feb 12, 2019
I agree with a general concern about reproducibility. And the successful transformation have this already - in other cases all fixture tests fill fail randomly, which doesn't happen.
@creachadair The problem is that the issue @bzz found is not on an error path. The main code path does this random iteration for performance reasons. Sorting keys for each node on every transform step and for every suboperation will have a very severe performance impact.
The real question is: why it returns different statuses? Are there any cases when it parses the file for the first time and fails the second time? If so - it's a bug in DSL or SDK. Unrelated to iteration order.
creachadair commentedon Feb 12, 2019
I agree it doesn't make sense to sort each time. What I guess I'm not clear about is where order matters: If order is only observed at the point where a result is reported to the caller, then it should be fine to sort the results once, at the point of return (e.g., a gRPC handler maybe).
If order matters at other times, then we should consider using a different data structure. But it sounds like the real issue is just that the final return value is variable, is that correct?
dennwc commentedon Feb 12, 2019
It's still not clear to me if the bug that @bzz found is the same as what @zurk originally reported.
Because if it's the same, it means that the DSL terminates the transform half-way because of an error and then reports different results because of the random iteration order (different branches visited before an error condition).
But if that's not the same, it means that some annotation is flaky and should be fixed.
bzz commentedon Feb 22, 2019
I will get back to this as soon as driver is migrated to latest SDK and missing annotations are added (#57) and post the updated results.
vmarkovtsev commentedon Mar 8, 2019
@bzz We are going to port to 2.7.0 (the current 2.6.0 has some problems) in 🔥src-d/style-analyzer#666 😈 and then we will try to reproduce again. Hopefully, it will be healed automagically with the help of Satan. 🔥
bzz commentedon Mar 11, 2019
Awesome! This is still very useful and is open due to #62 right now so yeah 😈
bzz commentedon Mar 13, 2019
#62 fixed, #68 found and added to v2.7.0 scope - after that, this issue is not reproducible any more and everything works as expected!
bzz commentedon Mar 15, 2019
v2.7.0 is out now!
So far, this issue is not reproducible any more with go-client app from #54 (comment):
@zurk could you verify that on your side please?
zurk commentedon Mar 19, 2019
Yes, I can not reproduce the bug. Looks good, thank you!