Skip to content

Writing / Ruby on Rails

Musings from a lean stack: The timeout you borrowed is breaking a feature you forgot

A profile import failed for everyone for four days without a single crash or alert. The cause was a 12-second timeout borrowed from another feature. Caller-owned limits, dumb-but-reliable fallbacks, and alerts for polite failures.

Part 4 of 5 in the series **Musings from a lean stack: notes from building and running software alone, on one server and a small budget.

Some bugs crash a page. Those are the easy ones, because something tells you. The bug I want to write about never crashed anything. It just politely told every single person who tried a feature that it didn't work, for four days, and nothing alerted me.

The complaint

A member of my social app sent a message: they tried to import their LinkedIn profile from the PDF LinkedIn lets you download, and the app said it couldn't read the PDF.

I checked the logs expecting a broken file. Instead, every PDF import over the previous four days had failed, seventeen attempts in all, each with the same line:

text
[ProfileImport] AI: ApiError: provider unreachable: Net::ReadTimeout
Completed 422 Unprocessable Content in 12229ms

Twelve seconds, every time.

The cause: a timeout borrowed from a different feature

The importer reads the text out of the PDF, then asks an AI model to sort it into jobs, education, skills and so on. It reused the same small AI client I'd built for content moderation, which made sense at the time. Moderation calls are short: "is this post OK?" gets a one-line answer in a second or two. So the client had sensible defaults for that job: give up after 12 seconds, and allow a reply of about 600 tokens.

A whole career history is a different job. The model needed longer than 12 seconds to answer, and even a fast answer would have been cut off at 600 tokens, which isn't enough to hold a full profile. So the shared client failed in a perfectly reasonable way, for a use it was never tuned for.

Timeouts and size limits are product decisions, and they belong to the caller. A default that's right for one feature can be wrong for another.

Fix 1: let the caller choose

ruby
module AiClient
  DEFAULT_TIMEOUT = 12   # right for quick verdicts
  DEFAULT_MAX_TOKENS = 600

  def self.ask(system:, user:, timeout: DEFAULT_TIMEOUT, max_tokens: DEFAULT_MAX_TOKENS)
    http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
    http.use_ssl = true
    http.open_timeout = http.read_timeout = timeout
    # ... build the request with max_tokens and send it
  end
end

# The importer asks for what it actually needs:
AiClient.ask(system: PROMPT, user: profile_text, timeout: 45, max_tokens: 4000)

Fix 2: don't let one dependency be a single point of failure

The second change mattered more. If the AI is slow, down or switched off, the feature shouldn't die with it. LinkedIn's PDF has a predictable layout: a sidebar (contact, top skills, languages, certifications) and a main column (name, headline, summary, experience, education), with date ranges like "March 2022 - Present (2 years 9 months)". That's enough to write a plain rule-based reader as a fallback:

ruby
DATE_RANGE = /\A(?:(\w+)\s+)?(\d{4})\s*[-–]\s*(?:(Present)|(?:(\w+)\s+)?(\d{4}))/i

def positions(lines)
  lines.each_with_index.filter_map do |line, i|
    next unless (m = line.match(DATE_RANGE))
    { title: lines[i - 1], company: company_above(lines, i), current: m[3].present? }
  end
end

def read(text)
  from_ai(text) || LayoutReader.new(text).call   # AI first, rules if it fails
end

The fallback isn't as clever as the model. It doesn't need to be. It turns "nothing works" into "this works a bit less well", and the member reviews everything before it's saved anyway.

Fix 3: alert on failures that don't crash

My crash alerts never fired because nothing crashed. The controller caught the error and showed a friendly message, exactly as designed. So I added a second kind of alert: count real failures per feature, and tell me when one keeps failing.

ruby
module FeatureFailure
  def self.record(feature, reason)
    count = Rails.cache.increment("failures:#{feature}", 1, expires_in: 1.hour)
    return if count < 3
    AdminAlert.notify(key: "feature:#{feature}",
                      title: "#{feature} keeps failing (#{count} in the last hour)",
                      detail: reason)
  end
end

The important part is deciding what counts. A member uploading the wrong kind of file isn't a failure of the feature, so that raises a separate MemberError that never counts. A timeout or a bad answer does count.

What it cost

People asked whether reading a profile with AI was expensive enough to restrict. With a small model, one import costs a fraction of a US cent, and the fallback costs nothing. The money was never the problem. The silence was.

Lessons

  • Shared clients need caller-owned limits. Pass timeouts and size limits from the feature, not from whoever wrote the client first.
  • Give every external dependency a fallback, even a dumb one. Less-good beats broken.
  • Friendly errors need alarms too. If the user sees an apology, you should see a count.
  • Read your 4xx logs, not just your 5xx. A wall of identical 422s is a bug wearing a polite face.

Comments

Leave a comment

No comments yet. Be the first.