sagemaker custom ml model in container

0

2022/11/09 05:21:09 [error] 11#11: *213 upstream prematurely closed connection while reading response header from upstream, client: 169.254.178.2, server: , request: "POST /invocations HTTP/1.1", upstream: "http://unix:/tmp/gunicorn.sock:/invocations", host: "169.254.180.2:8080" In this model we provide S3 audio object URL to given output audio S3 URL after processing. nginx.conf is as follows:

daemon off; # Prevent forking


pid /tmp/nginx.pid;
error_log /var/log/nginx/error.log;

events {
  # defaults
}

http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log combined;
  client_max_body_size 0;
  client_body_timeout 999;
  client_header_timeout 999;
  keepalive_timeout 999;
  fastcgi_buffers 8 16k;
  fastcgi_buffer_size 32k;
  fastcgi_connect_timeout 999;
  fastcgi_send_timeout 999;
  fastcgi_read_timeout 999;

  upstream gunicorn {
    #server 0.0.0.0:4602;
    server unix:/tmp/gunicorn.sock fail_timeout=0;
  }

  server {
    listen 8080 deferred;
    client_max_body_size 0;

    keepalive_timeout 1200s;
    proxy_read_timeout 3600s;

    location ~ ^/(ping|async-invocations|invocations) {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://gunicorn;
    }

    location / {
      return 404 "{}";
    }
  }
}```

and serve.py as follows:

```python
import multiprocessing
import os
import signal
import subprocess
import sys

cpu_count = multiprocessing.cpu_count()

model_server_timeout = os.environ.get('MODEL_SERVER_TIMEOUT', 999)
model_server_workers = int(os.environ.get('MODEL_SERVER_WORKERS', cpu_count))


def sigterm_handler(nginx_pid, gunicorn_pid):
    try:
        os.kill(nginx_pid, signal.SIGQUIT)
    except OSError:
        pass
    try:
        os.kill(gunicorn_pid, signal.SIGTERM)
    except OSError:
        pass

    sys.exit(0)


def start_server():
    print('Starting the inference server with {} workers.'.format(model_server_workers))
    print(model_server_timeout)
    # link the log streams to stdout/err so they will be logged to the container logs
    subprocess.check_call(['ln', '-sf', '/dev/stdout', '/var/log/nginx/access.log'])
    subprocess.check_call(['ln', '-sf', '/dev/stderr', '/var/log/nginx/error.log'])

    nginx = subprocess.Popen(['nginx', '-c', '/opt/program/nginx.conf'])
    gunicorn = subprocess.Popen(['gunicorn',
                                 '--timeout', str(model_server_timeout),
                                 '-k', 'sync',
                                 '-b', 'unix:/tmp/gunicorn.sock',
                                 '-w', str(model_server_workers),
                                 'wsgi:app'])

    signal.signal(signal.SIGTERM, lambda a, b: sigterm_handler(nginx.pid, gunicorn.pid))

    # If either subprocess exits, so do we.
    pids = set([nginx.pid, gunicorn.pid])
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break

    sigterm_handler(nginx.pid, gunicorn.pid)
    print('Inference server exiting')


# The main routine just invokes the start function.

if __name__ == '__main__':
    start_server()
asked a year ago700 views
1 Answer
0

The error means that it's upstream prematurely closed connection, here, Gunicorn. It's not quite related to NGINX. What's the timeout parameter you have configured when start gunicorn , i.e. --timeout option ? By default it's only 30s as per gunicorn doc https://docs.gunicorn.org/en/stable/settings.html#timeout it may change with versions but definitely won't be 3600s.

Also, if you are using SageMaker Endpoint, the maximum processing time for each invocation is only 60s. If your algorithm can't answer within 60s, SageMaker, as a client of your NGINX server in your container, will fail this request. There is not something we could configure this hard time out.

AWS
answered a year ago
  • timeout parameter is 999 and Processing time for model can't be more than 60s because it give this error before 60s. I tried still that error

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions