Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got conflict errors while making concurrent order requests. #3206

Closed
bsdelf opened this issue Feb 8, 2023 · 11 comments
Closed

Got conflict errors while making concurrent order requests. #3206

bsdelf opened this issue Feb 8, 2023 · 11 comments

Comments

@bsdelf
Copy link

bsdelf commented Feb 8, 2023

Bug report

Describe the bug

Got conflict errors while making concurrent order requests.

System information

Medusa version (including plugins):

  • "@medusajs/medusa-cli": "^1.3.6"
  • npx medusa -v: 1.7.5

Node.js version: v16.19.0
Database: PostgreSQL 15.1
Operating system: Void Linux (Kernel 6.1.9_1)
Browser (if relevant): none

Steps to reproduce the behavior

  1. create two customer accounts and save cookies for further usage
  2. go to "product_variant" table
    1. find the row titled as 'One Size"
    2. update the "inventory_quantity" field to 10000
    3. copy the "id" field for further usage
  3. create a k6 script which does following stuffs
    1. according to virtual user id injected by k6, load corresponding cookie obtained by step 1
    2. attach cookie to http header and make requests as follows (abort on any error)
      1. POST "/store/carts"
      2. POST "/store/carts/{cart_id}/payment_sessions"
      3. POST "/store/carts/{cart_id}/complete"
  4. execute the script with 2 virtual users, each one execute for 10 iterations, without time limit.

Expected behavior

20 orders should be created

Actual result

The APIs mentioned in 3.2.2 or 3.2.3 fail randomly,

k6 script outputs:

INFO[0001] http://localhost:9000/store/carts/cart_01GRR6S5VN5WCD7RXYHBGQNRT4/payment-sessions 409 {"code":"invalid_state_error","type":"conflict","message":"The request conflicted with another request. You may retry the request with the provided Idempotency-Key."}  source=console
INFO[0002] http://localhost:9000/store/carts/cart_01GRR6S61C7YYXRCAD1GHKV177/payment-sessions 409 {"code":"invalid_state_error","type":"conflict","message":"The request conflicted with another request. You may retry the request with the provided Idempotency-Key."}  source=console
INFO[0002] http://localhost:9000/store/carts/cart_01GRR6S6562BS8AJ75R4ZPQ2H0/complete 409 {"code":"invalid_state_error","type":"conflict","message":"The request conflicted with another request. You may retry the request with the provided Idempotency-Key."}  source=console
INFO[0002] http://localhost:9000/store/carts/cart_01GRR6S6C5BKQXHHTV9YM2PRKM/complete 409 {"code":"invalid_state_error","type":"conflict","message":"The request conflicted with another request. You may retry the request with the provided Idempotency-Key."}  source=console

backend log outputs:

::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S5VN5WCD7RXYHBGQNRT4/payment-sessions HTTP/1.1" 409 166 "-" "k6/0.42.0 (https://k6.io/)"
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S5VMQJSG3HSK3K3YG9YT/payment-sessions HTTP/1.1" 200 5288 "-" "k6/0.42.0 (https://k6.io/)"
>>> started
>>> tax_lines_created
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts HTTP/1.1" 200 4595 "-" "k6/0.42.0 (https://k6.io/)"
WARN: withTransaction called without custom implementation
WARN: withTransaction called without custom implementation
>>> payment_authorized
WARN: withTransaction called without custom implementation
WARN: withTransaction called without custom implementation
>>> finished
QueryFailedError: could not serialize access due to read/write dependencies among transactions
    at QueryFailedError.TypeORMError [as constructor] (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/error/TypeORMError.js:9:28)
    at new QueryFailedError (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/error/QueryFailedError.js:13:28)
    at PostgresQueryRunner.<anonymous> (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:281:31)
    at step (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:193:27)
    at Object.throw (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:174:57)
    at rejected (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:165:69)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  query: 'SELECT "IdempotencyKey"."id" AS "IdempotencyKey_id", "IdempotencyKey"."idempotency_key" AS "IdempotencyKey_idempotency_key", "IdempotencyKey"."created_at" AS "IdempotencyKey_created_at", "IdempotencyKey"."locked_at" AS "IdempotencyKey_locked_at", "IdempotencyKey"."request_method" AS "IdempotencyKey_request_method", "IdempotencyKey"."request_params" AS "IdempotencyKey_request_params", "IdempotencyKey"."request_path" AS "IdempotencyKey_request_path", "IdempotencyKey"."response_code" AS "IdempotencyKey_response_code", "IdempotencyKey"."response_body" AS "IdempotencyKey_response_body", "IdempotencyKey"."recovery_point" AS "IdempotencyKey_recovery_point" FROM "idempotency_key" "IdempotencyKey" WHERE "IdempotencyKey"."id" IN ($1)',
  parameters: [ 'ikey_01GRR6S63VNDWBS0CZFRX9QKD6' ],
  driverError: error: could not serialize access due to read/write dependencies among transactions
      at Parser.parseErrorMessage (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:287:98)
      at Parser.handlePacket (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:126:29)
      at Parser.parse (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:39:38)
      at Socket.<anonymous> (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/index.js:11:42)
      at Socket.emit (node:events:513:28)
      at Socket.emit (node:domain:489:12)
      at addChunk (node:internal/streams/readable:315:12)
      at readableAddChunk (node:internal/streams/readable:289:9)
      at Socket.Readable.push (node:internal/streams/readable:228:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
    length: 288,
    severity: 'ERROR',
    code: '40001',
    detail: 'Reason code: Canceled on identification as a pivot, during conflict out checking.',
    hint: 'The transaction might succeed if retried.',
    position: undefined,
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'predicate.c',
    line: '4099',
    routine: 'CheckForSerializableConflictOutNeeded'
  },
  length: 288,
  severity: 'ERROR',
  code: '40001',
  detail: 'Reason code: Canceled on identification as a pivot, during conflict out checking.',
  hint: 'The transaction might succeed if retried.',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'predicate.c',
  line: '4099',
  routine: 'CheckForSerializableConflictOutNeeded'
}
error:   Reason code: Canceled on identification as a pivot, during conflict out checking.
Error: Reason code: Canceled on identification as a pivot, during conflict out checking.
    at formatException (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/@medusajs/medusa/dist/utils/exception-formatter.js:27:20)
    at exports.default (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/@medusajs/medusa/dist/api/middlewares/error-handler.js:14:43)
    at Layer.handle_error (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:326:13)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:280:10)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:646:15
    at next (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:265:14)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:646:15 {
  type: 'conflict',
  code: undefined,
  date: 2023-02-08T09:47:48.767Z
}
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S5VMQJSG3HSK3K3YG9YT/complete HTTP/1.1" 200 4082 "-" "k6/0.42.0 (https://k6.io/)"
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S61C7YYXRCAD1GHKV177/payment-sessions HTTP/1.1" 409 166 "-" "k6/0.42.0 (https://k6.io/)"
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts HTTP/1.1" 200 4595 "-" "k6/0.42.0 (https://k6.io/)"
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts HTTP/1.1" 200 4595 "-" "k6/0.42.0 (https://k6.io/)"
WARN: withTransaction called without custom implementation
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S6562BS8AJ75R4ZPQ2H0/payment-sessions HTTP/1.1" 200 5288 "-" "k6/0.42.0 (https://k6.io/)"
WARN: withTransaction called without custom implementation
::ffff:127.0.0.1 - - [08/Feb/2023:09:47:48 +0000] "POST /store/carts/cart_01GRR6S66D3JXPP8YPJJGCXZBN/payment-sessions HTTP/1.1" 200 5288 "-" "k6/0.42.0 (https://k6.io/)"
>>> started
>>> started
>>> tax_lines_created
>>> tax_lines_created
WARN: withTransaction called without custom implementation
WARN: withTransaction called without custom implementation
WARN: withTransaction called without custom implementation
>>> payment_authorized
WARN: withTransaction called without custom implementation
QueryFailedError: could not serialize access due to read/write dependencies among transactions
    at QueryFailedError.TypeORMError [as constructor] (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/error/TypeORMError.js:9:28)
    at new QueryFailedError (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/error/QueryFailedError.js:13:28)
    at PostgresQueryRunner.<anonymous> (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:281:31)
    at step (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:193:27)
    at Object.throw (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:174:57)
    at rejected (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/tslib/tslib.js:165:69)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  query: 'INSERT INTO "payment"("id", "created_at", "updated_at", "swap_id", "cart_id", "order_id", "amount", "currency_code", "amount_refunded", "provider_id", "data", "captured_at", "canceled_at", "metadata", "idempotency_key") VALUES ($1, DEFAULT, DEFAULT, DEFAULT, $2, DEFAULT, $3, $4, DEFAULT, $5, $6, DEFAULT, DEFAULT, DEFAULT, DEFAULT) RETURNING "created_at", "updated_at", "amount_refunded"',
  parameters: [
    'pay_01GRR6S6ABTRBPR7WY22K07ZJ4',
    'cart_01GRR6S6562BS8AJ75R4ZPQ2H0',
    1000,
    'eur',
    'manual',
    '{"status":"authorized"}'
  ],
  driverError: error: could not serialize access due to read/write dependencies among transactions
      at Parser.parseErrorMessage (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:287:98)
      at Parser.handlePacket (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:126:29)
      at Parser.parse (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/parser.js:39:38)
      at Socket.<anonymous> (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/pg-protocol/dist/index.js:11:42)
      at Socket.emit (node:events:513:28)
      at Socket.emit (node:domain:489:12)
      at addChunk (node:internal/streams/readable:315:12)
      at readableAddChunk (node:internal/streams/readable:289:9)
      at Socket.Readable.push (node:internal/streams/readable:228:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
    length: 274,
    severity: 'ERROR',
    code: '40001',
    detail: 'Reason code: Canceled on identification as a pivot, during write.',
    hint: 'The transaction might succeed if retried.',
    position: undefined,
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'predicate.c',
    line: '4794',
    routine: 'OnConflict_CheckForSerializationFailure'
  },
  length: 274,
  severity: 'ERROR',
  code: '40001',
  detail: 'Reason code: Canceled on identification as a pivot, during write.',
  hint: 'The transaction might succeed if retried.',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'predicate.c',
  line: '4794',
  routine: 'OnConflict_CheckForSerializationFailure'
}
error:   Reason code: Canceled on identification as a pivot, during write.
Error: Reason code: Canceled on identification as a pivot, during write.
    at formatException (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/@medusajs/medusa/dist/utils/exception-formatter.js:27:20)
    at exports.default (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/@medusajs/medusa/dist/api/middlewares/error-handler.js:14:43)
    at Layer.handle_error (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:326:13)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:280:10)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:646:15
    at next (/home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:265:14)
    at /home/void/projects/medusa-demo/my-medusa-full/backend/node_modules/express/lib/router/index.js:646:15 {
  type: 'conflict',
  code: undefined,
  date: 2023-02-08T09:47:48.949Z
}

@Anan-Saadi
Copy link

Hey, I am having the same issue, did manage to find a solution ?

@alaarihan
Copy link

+1

@ellicodan
Copy link

I also encountered this error today when attempting to concurrently add multiple line items to a single cart.

@bqst
Copy link
Contributor

bqst commented Nov 23, 2023

+1

@adrien2p
Copy link
Member

adrien2p commented Dec 6, 2023

let me create a k6 script and take a look at this one

@adrien2p
Copy link
Member

adrien2p commented Dec 6, 2023

currently working on a fix, I ll keep you in touch

@adrien2p
Copy link
Member

adrien2p commented Dec 7, 2023

@bsdelf would you be able to test the snpashot once it is generate on that pr please

@hassanphp
Copy link

I have an issue with Paypal payment method:
0|Bakcend-Server | WARN: withTransaction called without custom implementation
0|Bakcend-Server | error: An error occurred in initiatePayment:
0|Bakcend-Server | An error occurred while requesting Paypal API after 2 attempts
0|Bakcend-Server | Error: An error occurred in initiatePayment:

please confirm is this same issue as above?

@adrien2p
Copy link
Member

adrien2p commented Dec 9, 2023

@hassanphp as you can see from the original error displayed in this thread, it is not the same, looks like in your case there was an issue when requesting the paypal api. I suggest you to investigate a but and if it persists and does not comes from paypal, then you can open a separate pr with all the necessary informations

@hassanphp
Copy link

@adrien2p Thank you, Highly appreciated your quick response.

@sradevski
Copy link
Member

Hey, thanks for the report! Since v2 brought a lot of architectural and API changes on the backend, we will be closing this ticket since it no longer applies to our new setup, or the issue has already been fixed. If you are still facing issues with v1,
please open a new ticket and we will address it as soon as possible. Thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants