iachat/spec/enterprise/services
Tanmay Deep Sharma 0592cccca9
fix: prevent lost custom_attributes updates from concurrent jsonb writes (#14040)
## Linear ticket
https://linear.app/chatwoot/issue/CW-6834/billing-upgrade-didnt-work

## Description
A `customer.subscription.updated` Stripe webhook for account 76162
returned 200 OK but did not persist the new `subscribed_quantity`. Root
cause: a race condition between the webhook handler and
`increment_response_usage` (Captain usage counter), both doing
read-modify-write on the `custom_attributes` JSONB column. The webhook
wrote `quantity: 6`, then a concurrent `save` from
`increment_response_usage` overwrote the entire hash with stale data —
restoring `quantity: 5`.

Fix: use atomic `jsonb_set` so usage counter updates only touch the
single key they care about, instead of rewriting the whole
`custom_attributes` hash. `increment_custom_attribute` also performs the
increment in SQL, making concurrent increments correct as well.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- New regression spec in `handle_stripe_event_service_spec.rb` that
simulates concurrent webhook + `increment_response_usage` and asserts
both `subscribed_quantity` and `captain_responses_usage` survive
- Existing account, billing, captain, and topup specs all pass locally

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
2026-04-13 19:03:37 +07:00
..
captain fix(captain): display handoff message to customer in V2 flow (#13885) 2026-04-08 17:30:07 +05:30
cloudflare feat(cloud): Add support for viewing status of SSL in custom domains (#12011) 2025-07-30 10:52:47 -07:00
companies feat: Add company backfill migration for existing contacts (Part 1) (#12657) 2025-11-03 20:03:47 +05:30
contacts feat: Backend - Companies API endpoint with pagination and search (#12840) 2025-11-18 14:28:56 +05:30
enterprise fix: prevent lost custom_attributes updates from concurrent jsonb writes (#14040) 2026-04-13 19:03:37 +07:00
internal feat: legacy features to ruby llm (#12994) 2025-12-11 14:17:28 +05:30
llm fix: captain json parsing (#13708) 2026-03-05 15:43:21 +05:30
messages fix(messages): reduce audio transcription 400 retry noise (#13487) 2026-02-17 13:25:13 +05:30
sla fix: assignee_changed callback not getting triggered during conversation creation (#9334) 2024-05-06 11:48:17 -07:00
twilio feat(voice): Improved voice call creation flow [EE] (#12268) 2025-08-22 13:38:23 +02:00
voice feat(enterprise): add voice conference API (#13064) 2025-12-15 15:11:59 -08:00