Skip to content

segfault error on post request #814

@davidporfirio7

Description

@davidporfirio7

I'm working on a project using Chatwoot, and I'm integrating it with Typebot. However, I'm encountering a segmentation fault (segfault) when making a POST request to Typebot’s API. This is crashing my workers intermittently.

The crash seems unrelated to payload size — responses are typically under 100KB, well below the threshold. Sometimes the full bot flow runs without issues; other times, it crashes on the first message.

Context

I'm using HTTParty to make the requests. Here’s the main file handling the API calls:

module Toolzzbot
  class ApiClient
    include HTTParty

    BASE_URL = "https://typebot.io/api/v1".freeze
    TIMEOUT = 10
    MAX_RESPONSE_SIZE = 500_000

    def initialize(toolzzbot_id)
      @toolzzbot_id = toolzzbot_id
    end

    def start_chat(custom_attributes)
      url = "#{BASE_URL}/typebots/#{@toolzzbot_id}/startChat"
      begin
        response = HTTParty.post(
          url,
          headers: { 'Content-Type' => 'application/json' },
          body: { prefilledVariables: custom_attributes }.to_json,
          timeout: TIMEOUT
        )
        return response.parsed_response if response.success? && response['sessionId'].present?
      rescue => e
        # Intentionally suppressed
      end
      nil
    end

    def continue_chat(session_id, message_content)
      url = "#{BASE_URL}/sessions/#{session_id}/continueChat"
      begin
        safe_message = message_content.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
        response = HTTParty.post(
          url,
          headers: { 'Content-Type' => 'application/json' },
          body: { message: safe_message }.to_json,
          timeout: TIMEOUT
        )
        return nil if response.body.bytesize > MAX_RESPONSE_SIZE

        if response.success?
          begin
            result = JSON.parse(response.body)
            return result if result.is_a?(Hash) && result['messages'].is_a?(Array)
          rescue JSON::ParserError
            return nil
          end
        elsif response.code == 404 && response.parsed_response['code'] == 'NOT_FOUND'
          return { session_ended: true }
        end
      rescue => e
        # Intentionally suppressed
      end
      nil
    end
  end
end

Example Payloads

Start Chat

{
  "sessionId": "cmay0d2sk000z92rbdmtu3gbd",
  "resultId": "y4gf6be2icb1dn7wamhbommr",
  "typebot": {
    "id": "cmawjeigv000d7ko5y5hp54gt",
    "theme": {},
    "settings": {}
  },
  "messages": [{
    "id": "hufnw5m93wgy7bu80l3txlld",
    "type": "text",
    "content": {
      "richText": [{
        "type": "p",
        "children": [{
          "text": "olá"
        }]
      }]
    }
  }],
  "input": {
    "id": "h4u6ev48zicwim2lpgo5pwu9",
    "outgoingEdgeId": "reskt5boa39wtytnmuw1g69n",
    "type": "text input"
  }
}

Continue Chat

{
  "messages": [{
    "id": "brai0fhcax04o942reqi6iv2",
    "type": "text",
    "content": {
      "richText": [{
        "type": "p",
        "children": [{
          "text": "tudo bem?"
        }]
      }]
    }
  }],
  "input": {
    "id": "vt63oex2xa5rh40j7j85ukgo",
    "outgoingEdgeId": "bqleirc2ha6t98n7cndh9zgs",
    "type": "text input"
  }
}

Issues Observed

  • Workers crash with a segfault, but logs don’t show much detail.
  • Crashes are inconsistent – sometimes the same payload works fine.
  • Response size is small, and I've added guards for large payloads and encoding issues.

If anyone has insights on what could be causing a segfault in this context — possibly a memory issue with the JSON parsing or HTTParty internals — I’d appreciate the help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions