iTranslated by AI
Troubleshooting Know-How: What to Do When You Encounter a Bug in OSS
Introduction
This article describes the troubleshooting expertise for when you encounter bugs in OSS, written as concretely as possible based on actual examples in a story-like format. The goal is to help those who are not accustomed to dealing with such situations understand the flow from actual debugging and bug reporting to submitting a proposed fix.
Encountering a Bug
One day, an article titled Twitter client made with shell script (Bash) was posted to the Linux Advent Calendar 2016, which I was also participating in. Since I was interested in OAuth 1.0a used for Twitter API authentication and "shell-gei" (shell wizardry), I decided to read this article.
When I tried to use the Bash-based Twitter client called tweet.sh introduced there, the output was as follows:
$ ./tweet.sh showme
VMo6+umZ_b4E
F2tH*wHY?=P֛7><M XE<omitted below>$
Immediately, something was wrong. I expected JSON-formatted data regarding my Twitter account to be returned, but what actually came back was unintelligible binary data.
At that time, I happened to have a high interest in technologies outside my usual expertise[1], and I had a fair amount of free time, so I decided to try debugging it.
Verifying Reproducibility
In debugging, it is extremely important to be able to easily reproduce the same problem. While some bugs can always be reproduced anywhere with simple steps, others are like demons that occur irregularly only once every few years.
Fortunately, the reproducibility of the problem I encountered this time was 100%.
Narrowing Down the Problem Area
Next, I narrowed down exactly where in the script and what happened. Looking at the source code without a plan takes time and effort, and is inefficient.
Since Bash has an option -x to display execution logs, I decided to use it to identify where the problem was occurring.
$ bash -x ./tweet.sh showme
<omitted>
++ curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json
^_8B^H^@^@^@^@^@^@^@8ACVMo8E36^P8FD+8828AEumZ_896^D8F48D08EDb88F8BD4E8omitted8$
I found that the problem occurred when issuing a request to the Twitter API with the curl command.
Next, I checked whether the same problem occurred when executing the above command alone.
$ curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json
^_8B^H^@^@^@^@^@^@^@8ACVMo8E36^P8FD+8828AEumZ_896^D8F48D08EDb88F8BD4E8omitted8$
It did. Now I've narrowed down the reproduction program to just a single execution of curl.
Next, I investigated what the binary data was as much as I could. This is where the file command comes in handy.
$ ../tmp/bin/curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json | file -
/dev/stdin: gzip compressed data, from FAT filesystem (MS-DOS, OS/2, NT)
It seemed the output was data compressed with gzip. Let's try to extract it.
$ curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json | gunzip
{"id":<omitted>}$
The extracted result was JSON-formatted data. This was the output I originally wanted. I had a feeling that content body compression in the HTTP specification might be involved.
Verifying Reproducibility in Different Environments
Around the same time as the verification in the previous section, I asked a colleague in the same industry, who I happened to be chatting with, to try to reproduce the problem. It turned out that the problem did not reproduce in my friend's environment. This is extremely useful for narrowing down the issue.
Next, I checked the versions to compare my environment with my friend's.
In my environment (Debian testing(stretch)):
$ curl --version
curl 7.50.1 (x86_64-pc-linux-gnu) libcurl/7.50.1 GnuTLS/3.5.7 zlib/1.2.8 libidn/1.33 libssh2/1.7.0 nghttp2/1.17.0 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets
$
In my friend's environment (Ubuntu14.04):
$ curl --version
curl 7.35.0 (x86_64-pc-linux-gnu) libcurl/7.35.0 OpenSSL/1.0.1f zlib/1.2.8 libidn/1.28 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP
$
While there are minor differences, I focused on the fact that the version names were different in the first place. Not wanting to keep my friend involved in debugging forever, I checked if the same thing happened in my own environment using a specific version. I built and used upstream version 7.35.0. I will skip the build and installation steps as they are not the main point.
$ ../tmp/curl --version | head -1
curl 7.34.1-DEV (x86_64-pc-linux-gnu) libcurl/7.50.1-DEV OpenSSL/1.1.0c zlib/1.2.8 libidn/1.33 libssh2/1.7.0 nghttp2/1.17.0 librtmp/2.3
$ ../tmp/bin/curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json
{"id":...}$
JSON-formatted data was returned correctly.
Next, I checked if the same problem occurred with the latest upstream version.
$ ../tmp/bin/curl --version | head -1
curl 7.52.2-DEV (x86_64-pc-linux-gnu) libcurl/7.52.2-DEV OpenSSL/1.1.0c zlib/1.2.8 libssh2/1.7.0 nghttp2/1.17.0 librtmp/2.3
$ ../tmp/bin/curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json
^_<8B>^H^@^@^@^@^@^@^@<AC>VMo<E3>6^P<FD>+<82><AE>umZ_<96>^D<F4><D0><ED>b<8F><BD>4E<omitted>$
As expected, the problem was reproduced.
Collecting Differences Between Reproducible and Non-reproducible Environments
Now that I had both reproducible and non-reproducible environments, I compared the differences between them. Since the failed output was gzip-compressed, I had a hunch that differences in HTTP headers might be involved. Since curl has a -v option that can output detailed information such as HTTP headers attached to requests and responses, I decided to use it.
$ ../tmp/bin/curl --get --header 'Authorization: OAuth <omitted>' --data '' --silent https://api.twitter.com/1.1/account/verify_credentials.json -v >/dev/null
Here is a comparison of the output from both versions when the above command was issued.
$ diff -urNp ../out.{good,bad}
<omitted>
+* Using HTTP2, server supports multi-use
+* Connection state changed (HTTP/2 confirmed)
+* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
<omitted>
-> User-Agent: curl/7.34.1-DEV
+> User-Agent: curl/7.52.2-DEV
-< HTTP/1.1 200 OK
<omitted>
+< HTTP/2 200
<omitted>
+< content-encoding: gzip
Summarizing these differences in a table results in the following:
| Version | Access Protocol to Twitter API | Encoding format allowed by curl (Accept-Encoding header) | Encoding format of response body (Content-Encoding header) |
|---|---|---|---|
| 7.35.0 | http1.1 | Not specified | Not specified (indicates raw JSON data) |
| Latest version | http2 | Not specified | gzip |
This reveals the following:
- At some point after 7.35.0,
curlbegan accessing the Twitter API via HTTP/2 by default instead of HTTP/1.1. - When the Twitter API is accessed via HTTP/1.1 without an
Accept-Encodingheader, it returns an uncompressed response body. - When the same is accessed via HTTP/2 without an
Accept-Encodingheader, it returns a body compressed with gzip.
Further Simplification of the Reproduction Program
Since authentication did not seem related to reproducing the problem, I tried to see if it would still occur when accessing without authentication (without the Authorization tag) as follows.
Case for 7.35.0:
$ ../tmp/bin/curl --get --silent https://api.twitter.com/1.1/account/verify_credentials.json | file -
/dev/stdin: ASCII text, with no line terminators
$
Case for the latest version:
$ ../tmp/bin/curl --get --silent https://api.twitter.com/1.1/account/verify_credentials.json | file -
/dev/stdin: gzip compressed data, from FAT filesystem (MS-DOS, OS/2, NT)
$
Similar to the previous reproduction programs, the former resulted in text format, while the latter resulted in gzip format. Although omitted here, the differences in header output obtained by adding the -v option were also the same as before. Additionally, the output of the former was in JSON format, and running the latter's output through gunzip yielded the same result as the former.
Specification or Bug?
Returning to the topic of HTTP headers. It feels intuitively strange to receive gzip-compressed data even though no Accept-Encoding header was specified. I checked whether this intuition was correct by reading the specifications. Judging correctness based on one's own assumptions before reporting a bug is a surefire way to fail in communication.
The Accept-Encoding header and Content-Encoding header are defined in RFC7231 5.3.4 and RFC7231 3.1.2.2, respectively.
The section on the Accept-Encoding header states the following:
<omitted>
A request without an Accept-Encoding header field implies
that the user agent has no preferences regarding content-codings.
Although this allows the server to use any content-coding in a response,
it does not imply that the user agent will be
able to correctly process all encodings.
<omitted>
In essence, it means "If the user agent does not specify an Accept-Encoding header, the server may encode the body in any way. However, in that case, the user agent might not be able to decode it correctly." In short, returning a gzip-compressed body when no Accept-Encoding header is specified does not seem to be a violation of the specification.
Next, using the latest version, I checked what would happen if I sent a request explicitly disallowing any compression as follows:
$ curl --get --silent --header "Accept-Encoding: identity;q=1.0,*;q=0" https://api.twitter.com/1.1/account/verify_credentials.json | file -
/dev/stdin: gzip compressed data, from FAT filesystem (MS-DOS, OS/2, NT)
The output was still compressed. This is a problem.
Bug Report and Fix
The report destinations for this issue were twitter developers forum[2] and tweet.sh on GitHub.
While there is only one root cause, the content addressed to each place differs slightly. If you get this wrong, the recipient might respond with "Why are you telling us this here?", so care is needed. Let's look at exactly what was done for each.
Reporting to the Twitter Developers Forum
To the forum, I reported that "The problem is that accessing the Twitter API via HTTP/2 without allowing compression still returns a compressed response body."
Before reporting, I searched the forum's past logs to see if it was an existing issue. It turned out that mattn_jp, who is well-known for Vim, Go, and many other things, had already reported it.
I would have retreated if my information had no novelty, but in this case, the report stated:
Twitter API doesn't treat "Accept-Encoding: identity".
I'm guessing many twitter client that doesn't use browser may not work."
So, I added a comment that I could reproduce the issue with curl.
When doing this, it's necessary to concisely state what environment was used, what was done, what the expected result was, and what actually happened. You can't get people to act with a report like "I don't know why, but it just didn't work."
Regarding the Twitter API, there's nothing more a bug reporter can do. It's a matter of waiting for some kind of reply. Since the source code is not public, I can't perform an analysis deep into the implementation, nor can I propose a fix.
On January 4, 2017, an official staff member replied with "I will share it with the team", but the issue would remain neglected for a while after that. The continuation is written later in this article.
Reporting to tweet.sh on GitHub
To this project, I reported that "There is an issue where tweet.sh does not work correctly when using recent versions of curl." Unlike the Twitter API, tweet.sh is OSS, so I could perform an analysis deep into the implementation and propose a fix (though whether the author accepts the proposal is up to them). I tried going that far this time.
First, before reporting, I checked the list of issues and pull requests to see if there were existing reports or proposed fixes. There were none in this case.
Regarding the fix, there are various ways to do it, but this time I proposed a workaround to access the Twitter API using HTTP/1.1, just like older versions of curl did. Here is the pull request:
This pull request was merged within an hour. When I saw that, I felt it was the very dynamism of OSS. OSS is truly great (not a stealth marketing attempt). This concludes the main thread of the story.
As a side note, this pull request only touched upon the content of the bug and omitted the essential explanation of the fix method. This was an oversight. In my draft, I had words like:
Although this problem comes from twitter API's problem,
it wouldn't be fixed for a while (forever?).
Accessing to twitter API via http1.1 can be a workaround.
But they had disappeared at some point. A big blunder, my apologies.
Twitter Finally Implements a Fix
About a year after I noticed this problem—and about two years after mattn_jp's original report—on January 3, 2018, an announcement was finally made by Twitter that the issue had been fixed. When I performed a reproduction test, it was indeed fixed.
$ ./curl --get --http2 https://api.twitter.com/1.1/account/verify_credentials.json
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
The ticket was also closed a few days later, triggered by an inquiry from mattn_jp. With this, the root cause—the issue on Twitter's side—was also resolved. All's well that ends well.
Conclusion
You don't need to do what I described in this article every time. It's best to weigh your motivation against your time and decide how far you'll go. It's okay to stop halfway if you get bored. Feedback is not an obligation but purely voluntary, and enjoying yourself should be the top priority. I rarely go as far as creating a fix myself, mostly because there are usually other things I'd rather be doing.
One more point. Since I am working outside my usual field, the debugging methods above are likely not the most optimal[3]. Nevertheless, to preserve the "live" feeling of how someone actually goes through trial and error when encountering a problem, I deliberately described the actual steps almost exactly as they were.
-
My main job is a Linux kernel developer. ↩︎
-
A place for discussing software development related to Twitter. ↩︎
-
For example, there are quite a few abno^H^H excellent engineers who can tell the moment they see the initial binary output, "This is gzip-compressed data, and it will be JSON after extraction." ↩︎
Discussion