Forums

Error code: 502-backend

Hello, pythonanywhere team. I think there may be a problem with my account. For my Django project, it is ok to run locally (or at least the local env will alert me when an error is encountered). But when online, I kept getting error: 502-backend error, and there was no more useful information in the error log and server log that could point me to the error, Even if I set debug=True.

The error log:

[ERROR 2021-02-17 11:23:09,045 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,045 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,045 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,045 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,044 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,044 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,044 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,044 user_wsgi_wrapper.py:165] [ERROR 2021-02-17 11:23:09,044 user_wsgi_wrapper.py:165] Error running WSGI application

repeats countless times;

the server log:

2021-02-17 11:23:09   File "/bin/user_wsgi_wrapper.py", line 165, in write
2021-02-17 11:23:09     self.logger.error(line)
2021-02-17 11:23:09   File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
2021-02-17 11:23:09     self._log(ERROR, msg, args, **kwargs)
2021-02-17 11:23:09   File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
2021-02-17 11:23:09     self.handle(record)
2021-02-17 11:23:09   File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
2021-02-17 11:23:09     self.callHandlers(record)
2021-02-17 11:23:09   File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
2021-02-17 11:23:09     hdlr.handle(record)
2021-02-17 11:23:09   File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
2021-02-17 11:23:09     self.emit(record)
2021-02-17 11:23:09   File "/bin/user_wsgi_wrapper.py", line 110, in emit
2021-02-17 11:23:09     format_with_millis(datetime.now()), self.format(record)
2021-02-17 11:23:09   File "/bin/user_wsgi_wrapper.py", line 96, in format_with_millis
2021-02-17 11:23:09     return "{:%Y-%m-%d %H:%M:%S},{:03d}".format(
2021-02-17 11:23:09 RecursionError: maximum recursion depth exceeded while calling a Python object

Same repeated countless times.

At first I thought it was a problem with my code. I checked my code for a long time but found nothing. Until I ran a very simple code, but this error also appeared, then I realize that it should not be the code error.

I started to check my account settings, and there seemed to be no problems, but I found that django commands were no longer working. When I run python manage.py (without parameters), it will remind me of the available parameters; but when I run python manage.py makemigrations/createsuperuser... with any parameters, it shows the parameters are incorrect.

I have asked about the system environment problem before, and I tried to solve it myself before that. I don’t know if I used the wrong command, which caused problems(I'm sorry, I might be the bad guy). I have never used Linux before. So I have no idea about the situation.

Thank you in advance.

If manage.py is not running the way you expect it to, then you are probably not running it in the same version of Python/virtualenv that you are running your web app in.

For the maximum recursion depth from logging error, that usually occurs because you are calling django.setup in your web app. It is not necessary to call django.setup in your web app and it breaks the logging when you do. If you are not using django.setup in your code, it can also be caused by some other thing that you are doing messing around with the stderr stream. If that is the case, there may be clues at the start of that chain of exceptions that indicates what is causing the problem.

I think I know what the problem with manage.py is that it conflicts with a library, so when I put a # on the line where the library was imported, I can use manage.py command. It's kind of weird.

About error code: 502-backend, I didn't use django.setup. The top of the log says:

2021-02-16 22:59:44,534: Internal Server Error: /search
Traceback (most recent call last):
File "/home/means/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/means/venv/lib/python3.8/site-packages/django/utils/deprecation.py", line 113, in __call__
response = self.process_request(request)
File "/home/means/venv/lib/python3.8/site-packages/django/middleware/common.py", line 53, in 
process_request
if self.should_redirect_with_slash(request):
File "/home/means/venv/lib/python3.8/site-packages/django/middleware/common.py", line 71, in 
should_redirect_with_slash
not is_valid_path(request.path_info, urlconf) and
File "/home/means/venv/lib/python3.8/site-packages/django/urls/base.py", line 154, in is_valid_path
resolve(path, urlconf)
File "/home/means/venv/lib/python3.8/site-packages/django/urls/base.py", line 25, in resolve
return get_resolver(urlconf).resolve(path)
File "/home/means/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 545, in resolve
for pattern in self.url_patterns:
File "/home/means/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/means/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/means/venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/means/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/home/means/venv/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/means/means/means/urls.py", line 22, in <module>
import predict.views as predict_views
File "/home/means/means/predict/views.py", line 51, in <module>
ocr = PaddleOCR(
File "/home/means/venv/lib/python3.8/site-packages/paddleocr/paddleocr.py", line 205, in __init__
maybe_download(postprocess_params.det_model_dir, model_urls['det'])
File "/home/means/venv/lib/python3.8/site-packages/paddleocr/paddleocr.py", line 103, in maybe_download
download_with_progressbar(url, tmp_path)
File "/home/means/venv/lib/python3.8/site-packages/paddleocr/paddleocr.py", line 84, in 
download_with_progressbar
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)
File "/home/means/venv/lib/python3.8/site-packages/tqdm/std.py", line 1098, in __init__
self.refresh(lock_args=self.lock_args)
File "/home/means/venv/lib/python3.8/site-packages/tqdm/std.py", line 1326, in refresh
self.display()
File "/home/means/venv/lib/python3.8/site-packages/tqdm/std.py", line 1473, in display
self.moveto(pos)
File "/home/means/venv/lib/python3.8/site-packages/tqdm/std.py", line 1422, in moveto
self.fp.write(_unicode('\n' * n + _term_move_up() * -n))
File "/home/means/venv/lib/python3.8/site-packages/tqdm/utils.py", line 143, in inner
return func(*args, **kwargs)
File "/bin/user_wsgi_wrapper.py", line 165, in write
self.logger.error(line)
File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
self.handle(record)
File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
self.callHandlers(record)
File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
hdlr.handle(record)
File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
self.emit(record)
File "/usr/lib/python3.8/logging/__init__.py", line 1084, in emit
stream.write(msg + self.terminator)
File "/bin/user_wsgi_wrapper.py", line 165, in write
self.logger.error(line)

I have no idea. And I think the log has not been updated again, it is still stop in the previous error

Then my guess is that paddleocr is doing something to the output streams and that is what is breaking the logging. It's probably designed as a command-line program (or you're using it that way). Perhaps you can disable the progress bar for downloads, or do the downloading outside of your web app.

This is where I feel strange, because I've already downloaded the model. In bash command line, I've tried to import the model, and it works fine. But in WebApp, it has to be redownloaded. I have checked the Settings of the working environment and virtual environment, and there is nothing wrong with them. The path to importing the model is also correct, so I am confused. I asked about the system environment before, and I'm not sure that I modified something that should not have been modified, which caused problems.

bash line:

>>> model = PaddleOCR(
     det_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_det_infer',
     rec_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_rec_infer',
     # rec_char_dict_path='',
     cls_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_cls_infer',
     use_angle_cls=True,
 )
 ...
 E0218 02:00:28.551189   323 analysis_config.cc:73] Please compile with gpu to EnableGpu()
 E0218 02:00:28.826700   323 analysis_config.cc:73] Please compile with gpu to EnableGpu()
 E0218 02:00:28.993677   323 analysis_config.cc:73] Please compile with gpu to EnableGpu()
 >>> model 
 <paddleocr.paddleocr.PaddleOCR object at 0x7f332b1d2190>

In webapp code:

ocr = PaddleOCR(
        det_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_det_infer',
        rec_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_rec_infer',
        # rec_char_dict_path='',
        cls_model_dir='/home/xxxxx/xxxxx/xxxxx/static/xxxxx/models/ppocr_mobile_v1.1_cls_infer',
        use_angle_cls=True,
    )

The path is exactly the same.

Does it require GPU? We do not have GPUs on our servers.

This is the CPU version.

Anyway, output streams behave differently in the console and in the web apps, and it looks like whatever your code is doing, conflicts with how our logging is working, leading to the errors as above. So the code could be working fine in the console about error in the web app.

Dear pythonanywhere team, when we were discussing, this problem was solved itself, and I didn't do anything. Thank you for your help.

However, there is still a problem with error log writing. An error log can be written many times.

File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
    self.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
    self.emit(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1084, in emit
    stream.write(msg + self.terminator)
  File "/bin/user_wsgi_wrapper.py", line 165, in write
    self.logger.error(line)
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
    self.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
    self.emit(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1084, in emit
    stream.write(msg + self.terminator)
  File "/bin/user_wsgi_wrapper.py", line 165, in write
    self.logger.error(line)
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
    self.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
    self.emit(record)
  File "/bin/user_wsgi_wrapper.py", line 110, in emit
    format_with_millis(datetime.now()), self.format(record)
  File "/bin/user_wsgi_wrapper.py", line 96, in format_with_millis
    return "{:%Y-%m-%d %H:%M:%S},{:03d}".format(
RecursionError: maximum recursion depth exceeded while calling a Python object
2021-02-19 00:29:55,553: [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/
2021-02-19 00:29:55,553: [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/
2021-02-19 00:29:55,554: [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/
2021-02-19 00:29:55,554: [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/
2021-02-19 00:29:55,554: [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/
2021-02-19 00:29:55,554: [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,554 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [ERROR 2021-02-19 00:29:55,553 user_wsgi_wrapper.py:165] [INFO 2021-02-19 00:29:54,348 log.py:224] OK: /home/

This is part of the log code, I wonder if there is any solution to this problem. Thanks.

What appears to be happening is that the Python logging system has been configured with a loop so that when you try to log something, it goes to (say) hander A, which is configured to delegate logging to handler B, which is in turn configured to delegate logging to handler A.

Are you doing any setup for the Python logging module in your code?

No, there is no logging code in my app. And i think there should be no code related to this. Very strange, I set debug to true, and then tested several known errors, all of them are recorded in this way, and raise 502-backend error instead.

It must be relaltaed to one of the packages you import.