Forums

502 error I can't understand why

This is the latest error message I got: OSError: write error

And the site by itself works it's just this button that makes things funky. This is what my server log says:

*** Starting uWSGI 2.0.20 (64bit) on [Fri Jul 21 18:50:40 2023] *** 2023-07-21 18:50:43 compiled with version: 9.4.0 on 22 July 2022 18:35:26 2023-07-21 18:50:43 os: Linux-5.15.0-1021-aws #25~20.04.1-Ubuntu SMP Thu Sep 22 13:59:08 UTC 2022 2023-07-21 18:50:43 nodename: green-liveweb39 2023-07-21 18:50:43 machine: x86_64 2023-07-21 18:50:43 clock source: unix 2023-07-21 18:50:43 pcre jit disabled 2023-07-21 18:50:43 detected number of CPU cores: 4 2023-07-21 18:50:43 current working directory: /home/laurasfelix28 2023-07-21 18:50:43 detected binary path: /usr/local/bin/uwsgi 2023-07-21 18:50:43 *** dumping internal routing table *** 2023-07-21 18:50:43 [rule: 0] subject: path_info regexp: .svgz$ action: addheader:Content-Encoding:gzip 2023-07-21 18:50:43 *** end of the internal routing table *** 2023-07-21 18:50:43 chdir() to /home/laurasfelix28/ 2023-07-21 18:50:43 your processes number limit is 256 2023-07-21 18:50:43 your memory page size is 4096 bytes 2023-07-21 18:50:43 detected max file descriptor number: 123456 2023-07-21 18:50:43 building mime-types dictionary from file /etc/mime.types... 2023-07-21 18:50:43 567 entry found 2023-07-21 18:50:43 lock engine: pthread robust mutexes 2023-07-21 18:50:43 thunder lock: disabled (you can enable it with --thunder-lock) 2023-07-21 18:50:43 uwsgi socket 0 bound to UNIX address /var/sockets/laurasfelix28.pythonanywhere.com/socket fd 3 2023-07-21 18:50:43 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0] 2023-07-21 18:50:43 *** Python threads support is disabled. You can enable it with --enable-threads *** 2023-07-21 18:50:43 Python main interpreter initialized at 0x5581da9f6e80 2023-07-21 18:50:43 your server socket listen backlog is limited to 100 connections 2023-07-21 18:50:43 your mercy for graceful operations on workers is 60 seconds 2023-07-21 18:50:43 setting request body buffering size to 65536 bytes 2023-07-21 18:50:43 mapped 501384 bytes (489 KB) for 2 cores 2023-07-21 18:50:43 *** Operational MODE: preforking *** 2023-07-21 18:50:43 initialized 54 metrics 2023-07-21 18:50:43 WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x5581da9f6e80 pid: 1 (default app) 2023-07-21 18:50:43 *** uWSGI is running in multiple interpreter mode *** 2023-07-21 18:50:43 gracefully (RE)spawned uWSGI master process (pid: 1) 2023-07-21 18:50:43 spawned uWSGI worker 1 (pid: 2, cores: 1) 2023-07-21 18:50:43 spawned 2 offload threads for uWSGI worker 1 2023-07-21 18:50:43 spawned uWSGI worker 2 (pid: 5, cores: 1) 2023-07-21 18:50:43 metrics collector thread started 2023-07-21 18:50:43 spawned 2 offload threads for uWSGI worker 2 2023-07-21 18:50:43 announcing my loyalty to the Emperor...

The 'OSError: write error' message is logged when a client disconnects from the server before the server has finished sending the response. This can happen because the site is slow and a user stops the page from loading or because the timeout on a client is too small.

Slowness can be caused by 2 things:

  1. You do not have enough workers to handle the traffic that is going to your site at particular times. When all of your workers are busy, any new requests are placed in a queue and have to wait for a worker to be free.

  2. Your code is doing something that is inherently slow.

You can investigate this by finding the views that are slow. Your access logs have a response-time for every access - it includes the time that a request spent in the queue waiting for a worker. If you add timing logging to the views that appear to be slow (prints to stderr will appear in your error log), you will be able to see where the views are spending their time and tell how long each request spent in the queue. Then you can either add workers or use the timing information to optimize your code so that it runs more quickly.