Skip to content

Conversation

glennmatthews
Copy link
Contributor

Fix #665 and fix #714 by adding try/except logic for the case where the SQL query params contain non-unicode binary data. The observed behavior with this fix is that queries involving such data will not render "explain" content in django-silk, which I think is an acceptable compromise for now versus trying to actually fix the logic here to correctly handle binary data.

I noticed as a side effect of the above bug that (with a long lived server process), django-silk's wrapped execute_sql function was being applied to requests that shouldn't be processed by django-silk (i.e. _should_intercept() returns False). This is because the wrapper wasn't being removed after processing a request, so all subsequent requests received by the process after any one _should_intercept() == True request were still getting the wrapper code. I've fixed this as well by adding appropriate logic in process_response() to revert the function wrapper when done with a request.

Copy link

codecov bot commented Jul 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.32%. Comparing base (25fc70a) to head (2dff0be).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #798      +/-   ##
==========================================
+ Coverage   87.26%   87.32%   +0.05%     
==========================================
  Files          53       53              
  Lines        2160     2169       +9     
==========================================
+ Hits         1885     1894       +9     
  Misses        275      275              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@albertyw albertyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some test cases here to test the logic for non-decodable strings (https://github.com/albertyw/django-silk/blob/master/project/tests/test_execute_sql.py) and that the interception can be added/removed (https://github.com/albertyw/django-silk/blob/master/project/tests/test_silky_middleware.py)

@glennmatthews
Copy link
Contributor Author

Thanks for the feedback! I'll try to find time to address it this week.

@glennmatthews
Copy link
Contributor Author

Hi @albertyw, I believe I've addressed the comments at this time. Any chance of getting this re-reviewed soon?

@albertyw albertyw mentioned this pull request Aug 10, 2025
@@ -77,7 +83,7 @@ def execute_sql(self, *args, **kwargs):
return iter([])
else:
return
sql_query = q % tuple(force_str(param) for param in params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #617 . Let's keep this as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll need to re-add the try/except logic here then to catch the case where force_str raises a UnicodeDecodeError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternately, should I go with an approach like #658?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #658 makes sense. It's possible it might result in invalid SQL but given this SQL is meant to be shown for debugging, I think a human can make that decision rather than just triggering an exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why #658 was closed without merging though. @bpascard

@glennmatthews
Copy link
Contributor Author

Latest diff changes the approach to add a force_str_with_fallback() function that explicitly handles a UnicodeDecodeError by representing the value as a hex instead. This appears to produce good behavior in the Silk UI, e.g.:

image

@albertyw albertyw merged commit e00b93c into jazzband:master Aug 13, 2025
93 checks passed
@frafrafrafra
Copy link

Guys... this Broke Django 4.2 with JSONField.. ---> force_str_with_fallback()
It's sending to db a Jsonfield in strings...

@albertyw
Copy link
Member

@frafrafrafra I can't seem to reproduce your issue. Do you have reproduction steps?

@SvenHellsten
Copy link

Guys... this Broke Django 4.2 with JSONField.. ---> force_str_with_fallback() It's sending to db a Jsonfield in strings...

Same for me...

@albertyw
Copy link
Member

Let's continue this in #806

albertyw added a commit to albertyw/django-silk that referenced this pull request Aug 17, 2025
albertyw added a commit to albertyw/django-silk that referenced this pull request Aug 19, 2025
…azzband#798)"

This partially reverts commit e00b93c.

The fixes in the middleware to restore the original `execute_sql`
function remain in place.
albertyw added a commit that referenced this pull request Aug 19, 2025
This partially reverts commit e00b93c.

The fixes in the middleware to restore the original `execute_sql`
function remain in place.
albertyw added a commit to albertyw/django-silk that referenced this pull request Aug 25, 2025
albertyw added a commit to albertyw/django-silk that referenced this pull request Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants