Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: use database in current context for file cache #18490

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

Merged
merged 1 commit into from
Jun 23, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 23, 2025

Using the db.Store when in a TX causes a deadlock for dbmem.
In production, this can cause a deadlock if at the current conn pool
limit.

Example:

var db database.Store
cache := files.NewFromStore(db)

// db.InTx consumes a pg connection
db.InTx(func(tx database.Store) {
  // If running 1 connection pool, this will block
  // until the tx is done. ! DEADLOCK !
  file, _ := cache.Acquire(...)
})

Copy link
Member Author

Emyrk commented Jun 23, 2025

@Emyrk Emyrk marked this pull request as ready for review June 23, 2025 02:47
@Emyrk Emyrk force-pushed the stevenmasley/deadlock_fetch branch 2 times, most recently from 353f193 to 2a37b9b Compare June 23, 2025 03:38
@Emyrk Emyrk requested a review from aslilac June 23, 2025 06:07
@Emyrk Emyrk force-pushed the stevenmasley/deadlock_fetch branch from 2a37b9b to a33fad3 Compare June 23, 2025 06:07
@Emyrk Emyrk force-pushed the stevenmasley/parameters_to_preview branch from 635c56b to b7985e3 Compare June 23, 2025 06:07
@Emyrk Emyrk requested a review from Copilot June 23, 2025 12:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the file cache implementation to use the current database context when acquiring files, thereby preventing potential deadlocks when running in a transaction. Key changes include:

  • Updating the Acquire method signatures in files/closer.go, files/cache.go, and related files to accept an explicit database.Store parameter.
  • Modifying tests in cache_test.go and other parts of the code to use the updated Acquire method.
  • Removing the deprecated NewFromStore constructor and switching to the new New constructor in coderd/coderd.go.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
coderd/files/closer.go Updated Acquire method signature to include a database parameter.
coderd/files/cache_test.go Adjusted test calls to use the new signature for Acquire.
coderd/files/cache.go Changed Acquire and prepare functions to accept db, and moved fetch logic to a package-level func.
coderd/dynamicparameters/render.go Updated Acquire calls with db parameter for template and module file acquisition.
coderd/coderd.go Replaced the deprecated NewFromStore with the new New constructor for FileCache.
Comments suppressed due to low confidence (1)

coderd/files/cache.go:140

  • Update the function documentation to reflect the addition of the db parameter and explain its role in avoiding deadlocks by providing the current database context.
func (c *Cache) Acquire(ctx context.Context, db database.Store, fileID uuid.UUID) (*CloseFS, error) {

@Emyrk Emyrk changed the base branch from stevenmasley/parameters_to_preview to graphite-base/18490 June 23, 2025 16:31
Using the db.Store when in a TX causes a deadlock for dbmem.
In production, this can cause a deadlock if at the current conn pool
limit.
@Emyrk Emyrk force-pushed the stevenmasley/deadlock_fetch branch from 9245e87 to 7349e1f Compare June 23, 2025 16:32
@Emyrk Emyrk force-pushed the graphite-base/18490 branch from 467aca3 to 659b787 Compare June 23, 2025 16:32
@graphite-app graphite-app bot changed the base branch from graphite-base/18490 to main June 23, 2025 16:32
@Emyrk Emyrk force-pushed the stevenmasley/deadlock_fetch branch 2 times, most recently from 043cd58 to 37c71f1 Compare June 23, 2025 16:45
@Emyrk Emyrk merged commit c1b35bf into main Jun 23, 2025
35 checks passed
@Emyrk Emyrk deleted the stevenmasley/deadlock_fetch branch June 23, 2025 16:58
@github-actions github-actions bot locked and limited conversation to collaborators Jun 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants