splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

ExpatError when logging in

Open Qmando opened this issue 10 years ago • 8 comments

When attempting to use the API, I would receive the following error with python2.6 and 2.7

Traceback (most recent call last):
  File "examples/search.py", line 115, in <module>
    main(sys.argv[1:])
  File "examples/search.py", line 72, in main
    service = client.connect(**kwargs_splunk)
  File "examples/../splunklib/client.py", line 321, in connect
    s.login()
  File "examples/../splunklib/binding.py", line 852, in login
    session = XML(body).findtext("./sessionKey")
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 964, in XML
    return parser.close()
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 1254, in close
    self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 1, column 0

I discovered that the cause of this issue was https://github.com/splunk/splunk-sdk-python/blob/master/splunklib/binding.py#L1341. When the HTTPConnection object is closed, so is the HTTPResponse object, causing read() to return an empty string. Removing this line fixed the problem.

I'm unsure of why this issue is affecting me and seemingly nobody else.

Qmando avatar Dec 04 '15 23:12 Qmando

@Qmando thanks for reporting the issue. A couple of questions:

  1. What specific Python versions are you using?
  2. What OS?
  3. What Splunk version?
  4. Do you have any load balancer or proxy between you and the splunkd you are connecting to?

itay avatar Dec 05 '15 00:12 itay

Tried with python 2.6.7 and python 2.7.6. Splunk is version 6.2. Ubuntu 10.04. It's behind an AWS load balancer.

Qmando avatar Dec 05 '15 00:12 Qmando

Thanks. I have a feeling that the LB is the issue - I think it's mucking with the connection in a way that is incompatible with how Python is doing the connection management.

The solution here is not simple: if we remove the .close(), the connection can leak for a while, causing other bad issues.

I'm going to try and set up a repro environment, but in the meantime, can you do me a favor, and try two things:

  1. See if this happens if you talk to your Splunk node directly and not through the LB
  2. Right after this line: response = connection.getresponse(), print out the headers in the response. I'm curious what the LB is returning.

itay avatar Dec 05 '15 00:12 itay

So I got an environment up in AWS with an AWS ELB in front of 3 search heads, and ran the SDK (and ensured it was running the code you saw the crash in). These are the headers I get:

[  
   ('content-length',
   '178'   ),
   ('x-content-type-options',
   'nosniff'   ),
   ('set-cookie',
   'splunkd_8089=xxxx'   ),
   ('expires',
   'Thu, 26 Oct 1978 00:00:00 GMT'   ),
   ('server',
   'Splunkd'   ),
   ('connection',
   'Close'   ),
   ('cache-control',
   'no-store, no-cache, must-revalidate, max-age=0'   ),
   ('date',
   'Sat, 05 Dec 2015 00:41:02 GMT'   ),
   ('x-frame-options',
   'SAMEORIGIN'   ),
   ('content-type',
   'text/xml; charset=UTF-8'   )
]

However, I did not see the crash you are seeing. What LB do you have set, and if it is an ELB, do you have any Keep-Alive options set? I want to see if the headers you're getting back are different than what I am seeing.

itay avatar Dec 05 '15 00:12 itay

[('content-length', '159'), ('x-content-type-options', 'nosniff'), ('expires', 'Thu, 26 Oct 1978 00:00:00 GMT'), ('server', 'Splunkd'), ('connection', 'keep-alive'), ('cache-control', 'no-store, no-cache, must-revalidate, max-age=0'), ('date', 'Sat, 05 Dec 2015 00:24:10 GMT'), ('x-frame-options', 'SAMEORIGIN'), ('content-type', 'text/xml; charset=UTF-8')]

I'll have to get back to you later on the direct connection testing and LB configuration settings.

Qmando avatar Dec 05 '15 00:12 Qmando

Thanks - that's incredibly useful.

It seems that the inclusion of keep-alive in the connection header is really messing us up in some bizarre way. I will try and set up an environment where I can reproduce that (I have some ideas), and then will try and debug it.

itay avatar Dec 05 '15 00:12 itay

@Qmando I was able to reproduce it and I believe I know what is going on. I have a proposed fix here:

https://github.com/splunk/splunk-sdk-python/pull/130

Could you let me know if it fixes your issue?

itay avatar Dec 10 '15 00:12 itay

This was released in v1.6.0

shakeelmohamed avatar Nov 18 '16 22:11 shakeelmohamed