title: Cloud function working locally but crashing in GCP (Function execution took 12681 ms, finished with status: 'crash')
body: <p>Here is a super abbreviated version of my code (I'll put a link to everything in comments)</p>
  
  <pre class="lang-py prettyprint-override"><code>import ...
  
  ...
  
  def main(data, context):
      log_client = logging.Client()
  
      log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'
  
      res = Resource(type="cloud_function",
                     labels={
                         "function_name": "refresh_classes",
                         "region": os.environ.get("FUNC_REGION")
                     })
      logger = log_client.logger(log_name.format(os.environ.get("PROJECT_ID")))
  
      db = sqlalchemy.create_engine(
          sqlalchemy.engine.url.URL(
              drivername="mysql+pymysql",
              username=os.environ.get("DB_USER"),
              password=os.environ.get("DB_PASS"),
              host=os.environ.get("DB_HOST"),
              port=3306,
              database=PRIMARY_TABLE_NAME
          ),
          pool_size=5,
          max_overflow=2,
          pool_timeout=30,
          pool_recycle=1800
      )
      start_time = perf_counter()
  
      check_if_table_exists(db)
  
      for i in range(START_IDX, END_IDX):
          print(i)
          logger.log_text(f"Checking class with id {i}", resource=res, severity="INFO")
  
          ...
  
      logger.log_text(f"Total seconds elapsed: {perf_counter() - start_time}", resource=res, severity="INFO")
  
  
  if __name__ == '__main__':
      main('data', 'context')
  </code></pre>
  
  <p>When I run the above cloud function locally, with my <code>GOOGLE_APPLICATION_CREDENTIALS</code> configured along with my local Cloud MySQL proxy set up, the verbose cloud logging comes through and the function finishes without a hitch, exactly as I would expect:</p>
  
  <p><a href="https://i.stack.imgur.com/zZxb5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zZxb5.png" alt="enter image description here"></a></p>
  
  <p>However, when I deploy the entire thing to GCP, and try to trigger it through the console (cloud messaging trigger), all I get is this in terms of logging:</p>
  
  <p><a href="https://i.stack.imgur.com/vrXNk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vrXNk.png" alt="enter image description here"></a></p>
  
  <p><strong>actual text</strong></p>
  
  <pre><code>{
   insertId: "******"  
   labels: {
    execution_id: "******"   
   }
   logName: "projects/******/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
   receiveTimestamp: "2020-05-29T22:11:13.435688367Z"  
   resource: {
    labels: {
     function_name: "******"    
     project_id: "******"    
     region: "us-central1"    
    }
    type: "cloud_function"   
   }
   severity: "DEBUG"  
   textPayload: "Function execution started"  
   timestamp: "2020-05-29T22:11:03.069889708Z"  
   trace: "projects/******/traces/******"  
  }
  
  {
   insertId: "******"  
   labels: {
    execution_id: "******"   
   }
   logName: "projects/******/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
   receiveTimestamp: "2020-05-29T22:11:16.331311285Z"  
   resource: {
    labels: {
     function_name: "******"    
     project_id: "******"    
     region: "us-central1"    
    }
    type: "cloud_function"   
   }
   severity: "DEBUG"  
   textPayload: "Function execution took 12362 ms, finished with status: 'crash'"  
   timestamp: "2020-05-29T22:11:15.430033249Z"  
   trace: "projects/******/traces/******"  
  }
  </code></pre>
  
  <p>*i don't really know what's sensitive information and what's not, so I just starred some random things</p>
  
  <p>As I'm writing this, I realize more logging could help, so I stuck a Google logger in between the logger setup, db setup, and the table check I run.</p>
  
  <p><strong>The function crashes before the Google logger is even set up.</strong></p>
  
  <p>So at this point I'm not really sure what could be breaking my function, and I don't know how to find out, seeing as Google Cloud logging is of no help. The error json has a <code>trace</code> property which looks really promising, because all I need at this point is a Python stack trace, but I don't know if there's a way to see that.</p>
  
  <p>I should note that I have environment variables configured through GCP's Cloud Function console.</p>
  
  <p><a href="https://i.stack.imgur.com/ZqCxR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZqCxR.png" alt="enter image description here"></a></p>
  
  <p>Principally, there are two things that would be helpful:</p>
  
  <ol>
  <li>how to see python stack trace of crashing cloud function</li>
  <li>what, specific to my application, could be causing it to exhibit this crashing behavior</li>
  </ol>