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

JSONResultsReader iterator called on oneshot search does not iterate with for loop.

Open jerryeblades opened this issue 1 year ago • 4 comments

Describe the bug JSONResultsReader iterator called on oneshot search does not iterate with for loop.

To Reproduce -run a oneshot search, the notice the for content/command[s] fails.

reader = JSONResultsReader(result_stream)
for item in reader:
    print(item)

Expected behavior iterate through the results and take action on.

Logs or Screenshots If applicable, add logs or screenshots to help explain your problem.

Splunk (please complete the following information):

  • Version: Splunk Enterprise 9.1.1
  • OS: Rocky Linux 9.6
  • Deployment: Distributed/Clustered SH's and IDX's architecture

SDK (please complete the following information):

  • Version: [2.1.0 and 2.1.1]
  • Language Runtime Version: Python 3.12.3
  • OS: Ubuntu 24.04.2 LTS

Additional context This issue is only impacting the "oneshot" type search, with "normal" type search successfully yields an iterable "reader" for reader = JSONResultsReader(result_stream).

The workaround is to do:

reader = JSONResultsReader(result_stream)
reader2 = list(reader)
for item in reader2:
    print(item)

jerryeblades avatar Oct 02 '25 20:10 jerryeblades

I am not able to reproduce with:

reader = results.JSONResultsReader(
    service.jobs.oneshot("| makeresults count=1000", output_mode="json")
)

for item in reader:
    print(item)

This code successfully prints the entire result.

Could you provide us with a complete reproducer?

mateusz834 avatar Oct 03 '25 08:10 mateusz834

index_query = '''search index=_internal earliest=-1s |head 250'''
searchlist=[index_query]

kwargs_results = {
        "output_mode": "json",
        "count": 0
        }

for search_query in searchlist :
    oneshot_results = service.jobs.oneshot(search_query, **kwargs_results)
    reader = results.JSONResultsReader(oneshot_results)
    for result in reader:
        print(result)

jerryeblades avatar Oct 03 '25 13:10 jerryeblades

I tried this on branch develop and it seems to work just fine, print(result) is reached, without using list(). Have you tried adjusting the: earliest=-1s to a bigger time range, like: earliest=-100s?

mateusz834 avatar Oct 06 '25 13:10 mateusz834

I tried this on branch develop and it seems to work just fine, print(result) is reached, without using list(). Have you tried adjusting the: earliest=-1s to a bigger time range, like: earliest=-100s?

Yes, this is only a sample query. All valid queries on my current setup, using oneshot query method and for loop described here do not show me results without first transforming JSONResultsReader oject to list first; other than oneshot works fine.

jerryeblades avatar Oct 06 '25 13:10 jerryeblades