You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apply env-backed configuration defaults for Data initialization
pkg/workflow/dataimpl.go
Modified NewData to initialize DataImpl with configuration that automatically loads environment variables.
Introduced default values for IN_MEMORY_THRESHOLD_BYTES using constants.SNYK_DEFAULT_IN_MEMORY_THRESHOLD_MB when environment variables are not explicitly set.
Ensured that explicit WithConfiguration options provided by the caller will override environment-backed defaults.
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review
Unit Mismatch 🟠 [major]
The default memory threshold is initialized using constants.SNYK_DEFAULT_IN_MEMORY_THRESHOLD_MB, but the inMemoryThreshold field is documented as being in 'bytes' (see line 21) and is used directly in byte-count comparisons (line 290). Given the constant name and the comment in pkg/app/app.go (line 347), the value is likely 512. This results in a default threshold of 512 bytes rather than 512 megabytes, causing nearly all byte-slice payloads to be unexpectedly written to disk.
Resource Leak 🟠 [major]
By removing the previous default threshold of -1 (which explicitly disabled disk spilling), this PR enables the feature by default for all Data objects. As shown in pkg/workflow/types.go, the Data interface lacks a Close() or Cleanup() method, and writeDataToDisk (line 296) does not implement any automatic deletion. This will cause applications to leak temporary files and potentially exhaust disk space, especially given the likely unit mismatch that triggers spills for very small payloads.
False positive: SNYK_DEFAULT_IN_MEMORY_THRESHOLD_MB is 512 * 1024 * 1024 bytes.
Resource leak
Half false.
Removing -1 enables the default 512 MB as app.initConfiguration. 512 MB is large enough memory to affirm that just very large byte payloads may spill.
The CLI removes the managed temp directory on exit, so after that there problem solved.
In any case, it is always possible to get the ols "in memory" behavior by passing WithConfiguration with -1 (see dataimpl_test.go:243 "caller WithConfiguration overrides env-backed default")
The configuration key configuration.IN_MEMORY_THRESHOLD_BYTES expects a value in bytes (as documented in the DataImpl struct), but it is being assigned constants.SNYK_DEFAULT_IN_MEMORY_THRESHOLD_MB. The constant name and related comments in pkg/app/app.go suggest a value in Megabytes (e.g., 512). Without a conversion to bytes (multiplication by 1024*1024), the resulting threshold will be extremely low (512 bytes), causing virtually all payloads to be written to disk. This will cause significant performance degradation due to unnecessary disk I/O for small payloads.
newDataWith now creates a new Configuration instance (which involves initializing a viper.Viper object) every time it is called. Since NewData and NewDataFromInput are fundamental constructors used frequently throughout the lifecycle of a workflow, this adds non-trivial CPU and memory overhead per data object. Consider using a shared default configuration or directly checking environment variables if WithConfiguration is omitted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Description
Provide description of this PR and changes.
Checklist
make test)make generate)make lint)go get github.com/snyk/go-application-framework@YOUR_LATEST_GAF_COMMITin thecliv2directory.go.modto point to your local GAF code.go mod tidyin thecliv2directory.go.modandgo.sumchanges.PR Type
Enhancement, Bug fix
Description
Improve default configuration loading for Data.
Prioritize environment variables for defaults.
Enhance test coverage for configuration handling.
Diagram Walkthrough
File Walkthrough
dataimpl.go
Apply env-backed configuration defaults for Data initializationpkg/workflow/dataimpl.go
NewDatato initializeDataImplwith configuration thatautomatically loads environment variables.
IN_MEMORY_THRESHOLD_BYTESusingconstants.SNYK_DEFAULT_IN_MEMORY_THRESHOLD_MBwhen environmentvariables are not explicitly set.
WithConfigurationoptions provided by the callerwill override environment-backed defaults.
dataimpl_test.go
Expand tests for Data configuration and environment variable handlingpkg/workflow/dataimpl_test.go
payloadLocationTypeandinMemoryThresholdBytesfor easier test assertions.
honored when configuration is omitted.
caller-provided configuration overrides environment settings.
NewDataFromInputto ensure it also inheritsenv-backed defaults.
MANUAL TESTING
Local build.
RELATED
There was a previous PR trying to approach this issue which includes a quite similar approach: there
WithConfiguration()checks ifIN_MEMORY_THRESHOLD_BYTEShas been set - if not, it assigns a value.This PR just sets the same default value as during the app bootstrap (512 MB).