question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to retry response on 503 in script?

See original GitHub issue

Env: mitmdump 0.18.2 (trying to migrate a script from 0.16)

Hello!

I’m trying to understand how I might use replay_request or something else to add proxy-level retries on certain errors, such as when a flaky backend throws a 503 error.

I’ve tried this:

$ mitmdump -p 8080 --no-http2 -s myscript.py

myscript.py:

from mitmproxy import ctx

def response(flow):
    # Retry once if the backend responded with 503:
    if flow.response.status_code == 503:
        f = ctx.master.duplicate_flow(flow)
        ctx.master.replay_request(f, block=True)

…but this seems to deadlock, I think because I don’t understand the way the main thread processes flows (or how exactly to replace the current flow.response).

Can you point me in the right direction here?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fotinakiscommented, Nov 21, 2016

Here’s what ended up working, feel free to let me know if something looks off here.

Notably: it seems that the response hook fires for every replayed request (which is the opposite of what I read you talk about on the forums), so I had to add a _num_retries tracker to prevent infinite recursion.

I also had to do flow.response = new_flow.response, which I’m not sure if that’s correct way to override the top-level response.

@concurrent
def response(flow):
  if flow.response.status_code == 503:
    retry_number = getattr(flow, '_num_retries', 0)

    if retry_number < MAX_REQUEST_RETRIES:
      new_flow = master.duplicate_flow(flow)
      setattr(new_flow, '_num_retries', retry_number + 1)
      master.replay_request(new_flow, block=True)
      flow.response = new_flow.response
    else:
      syslog.syslog(syslog.LOG_ERR, 'Permanent 503 from: %s' % flow.request.url)

Thanks for the help!

0reactions
maximusfoxcommented, Sep 3, 2020

Here’s what ended up working, feel free to let me know if something looks off here.

Notably: it seems that the response hook fires for every replayed request (which is the opposite of what I read you talk about on the forums), so I had to add a _num_retries tracker to prevent infinite recursion.

I also had to do flow.response = new_flow.response, which I’m not sure if that’s correct way to override the top-level response.

@concurrent
def response(flow):
  if flow.response.status_code == 503:
    retry_number = getattr(flow, '_num_retries', 0)

    if retry_number < MAX_REQUEST_RETRIES:
      new_flow = master.duplicate_flow(flow)
      setattr(new_flow, '_num_retries', retry_number + 1)
      master.replay_request(new_flow, block=True)
      flow.response = new_flow.response
    else:
      syslog.syslog(syslog.LOG_ERR, 'Permanent 503 from: %s' % flow.request.url)

Thanks for the help!

How to make script like this on 5.x version?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Status 503: is "Retry-After" necessary? - Stack Overflow
The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay...
Read more >
How can I return a 503 status in apache without invoking ...
Hm. I think invoking a whole interpreted scripting language to set a single line of the server response does qualify as hack. Setting...
Read more >
Retry-After - HTTP - MDN Web Docs
Retry -After · When sent with a 503 (Service Unavailable) response, this indicates how long the service is expected to be unavailable. ·...
Read more >
How to Fix the HTTP Error 503 Service Unavailable - Kinsta
The server MAY send a Retry-After header field to suggest an appropriate amount of time for the client to wait before retrying the...
Read more >
503 Service Unavailable Error: What It Is and How to Fix It
Application Code or Script Bugs ... If all else fails, check your code. A bug could be causing the 503 Service Unavailable Error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found