Repository navigation
Conversation
…e handler leaks Resolves b/584840147 (PR 1 of 3). Replaces java.util.logging.FileHandler inside PerConnectionFileHandler with a bounded NIO SeekableByteChannel writer enforcing 0700 on newly created directories (preserving existing shared directories and macOS root symlinks while rejecting nested symlinks), 0600 on log files, and a revisable 100 MiB default single-file cap (configurable via -Dcom.google.cloud.bigquery.jdbc.maxLogFileBytes). Stamps a typed ConnectionLogContext in ResultSet log parameters and gates per-connection log writers on synchronized active ConnectionFileSlot entries.
There was a problem hiding this comment.
Code Review
This pull request refactors the JDBC driver's logging infrastructure to support secure, per-connection file logging using NIO SeekableByteChannel directly, preventing lock file creation and enforcing strict POSIX file permissions. It also introduces a typed ConnectionLogContext to avoid parameter collisions during message formatting and ensures proper cleanup of executors and OpenTelemetry resources upon connection failure. The code review feedback identifies critical resource leaks in PerConnectionFileHandler where opened channels could be leaked if setting POSIX permissions or positioning the channel throws an exception. Additionally, a potential NullPointerException was flagged in BigQueryConnection's cleanup block if openTelemetry is null during a failed initialization.
| SeekableByteChannel channel = Files.newByteChannel(file, createNewOpts, attrs); | ||
| if (isPosix) { | ||
| Files.setPosixFilePermissions(file, OWNER_FILE_PERMS); | ||
| } | ||
| return channel; |
There was a problem hiding this comment.
If Files.setPosixFilePermissions throws an IOException or RuntimeException (e.g., due to filesystem restrictions or security managers), the newly opened SeekableByteChannel is leaked because it is never closed. Wrapping the permission setting in a try-catch block that closes the channel on failure prevents resource leaks.
SeekableByteChannel channel = Files.newByteChannel(file, createNewOpts, attrs);
if (isPosix) {
try {
Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
} catch (IOException | RuntimeException e) {
try {
channel.close();
} catch (IOException closeEx) {
e.addSuppressed(closeEx);
}
throw e;
}
}
return channel;| SeekableByteChannel channel = Files.newByteChannel(file, writeExistingOpts); | ||
| channel.position(channel.size()); | ||
| return channel; |
There was a problem hiding this comment.
If channel.position(channel.size()) throws an IOException (e.g., if the channel is concurrently closed or an I/O error occurs), the opened SeekableByteChannel is leaked. Wrapping the positioning call in a try-catch block that closes the channel on failure prevents resource leaks.
SeekableByteChannel channel = Files.newByteChannel(file, writeExistingOpts);
try {
channel.position(channel.size());
} catch (IOException | RuntimeException e) {
try {
channel.close();
} catch (IOException closeEx) {
e.addSuppressed(closeEx);
}
throw e;
}
return channel;| SeekableByteChannel channel = Files.newByteChannel(file, opts, attrs); | ||
| if (isPosix) { | ||
| Files.setPosixFilePermissions(file, OWNER_FILE_PERMS); | ||
| } | ||
| return channel; |
There was a problem hiding this comment.
If Files.setPosixFilePermissions throws an IOException or RuntimeException, the newly opened SeekableByteChannel is leaked. Wrapping the permission setting in a try-catch block that closes the channel on failure prevents resource leaks.
SeekableByteChannel channel = Files.newByteChannel(file, opts, attrs);
if (isPosix) {
try {
Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
} catch (IOException | RuntimeException e) {
try {
channel.close();
} catch (IOException closeEx) {
e.addSuppressed(closeEx);
}
throw e;
}
}
return channel;| } | ||
| BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId); | ||
| BigQueryJdbcOpenTelemetry.unregisterConnection(this.connectionId); | ||
| BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry); |
There was a problem hiding this comment.
In the finally block, this.openTelemetry can be null if the constructor fails before openTelemetry is initialized (or during its initialization). Calling releaseSdk with a null argument without a guard can cause a NullPointerException, which would mask the original exception that triggered the finally block. Adding a null check prevents this.
if (this.openTelemetry != null) {
BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry);
}References
- When using lazily initialized resources, ensure that teardown or close methods perform explicit null checks before invoking methods on them to prevent NullPointerException.
What & Why
PR 1 of 3 in the diagnostic logging hardening stack.
When diagnostic file logging is enabled (
LogLevel > 0),PerConnectionFileHandlerpreviously had three issues:Files.createDirectories) and.logfiles (new FileHandler(filePath, 0, 1, true)) inherited the OS defaultumask(typically0755for directories and0644for files) on shared POSIX hosts.%pattern expansion:FileHandlerwas configured withlimit = 0(unlimited file size), expanded%t/%h/%g/%upattern tokens in paths, and fell back to.1files using the defaultumaskwhenever.lckfiles collided.params[0]file-descriptor leaks: WhenBigQueryJdbcMdcwas empty,PerConnectionFileHandler.publish()treated anyStringinLogRecord.getParameters()[0]as an active connection ID viacomputeIfAbsent. Calls on closedResultSets, detachedStruct/Arrayinstances, orgoogle-cloud-bigqueryretry loggers (BigQueryRetryHelper/BigQueryRetryAlgorithmatFINEST) opened unclosed file writers permanently.Changes in this PR
SeekableByteChannelwriter with owner-only POSIX permissions (0700/0600): Replacesjava.util.logging.FileHandlerinsidePerConnectionFileHandlerwith a bounded NIOSeekableByteChannelwriter (BoundedLogFileWriter):rwx------(0700) without modifying permissions on pre-existing shared directories (such as/tmp), while rejecting symbolic links beneath top-level OS root links (supporting macOS/tmpand/varwhile blocking nested symlink traversal).CREATE_NEWwithrw-------(0600) and collision-safe naming (BQ-JDBC-<ts>-<attempt>-<shortUuid>.log), and global log files verify regular file type,NOFOLLOW_LINKS, and OS owner match (isolating byPROCESS_IDif a shared directory like/tmpalready contains another user'sBQ-JDBC-GLOBAL.log), eliminating.lckfiles,.10644fallbacks, and%pattern expansion by construction.100 * 1024 * 1024bytes, overridable via-Dcom.google.cloud.bigquery.jdbc.maxLogFileBytes), truncating the open0600channel in place when exceeded.FINEST/FINEdiagnostic history for debugging teams while preventing unbounded disk growth and preserving0600permissions on the open inode. This 100 MiB default is a decision that can be revised (or exposed as a connection URL property) based on team feedback.ConnectionLogContext& synchronized active-slot gate: Replaces rawStringstamping inBigQueryJdbcResultSetLoggerparams[0]with package-privateConnectionLogContext, stripsparams[0](checking{0..{9positional placeholders) inOpenTelemetryJulHandler.formatMessageso SQL single quotes and positional arguments are preserved, synchronizesBigQueryJdbcRootLogger.setLevel/setPath, and gates per-connection log writers on synchronized activeConnectionFileSlotentries withBigQueryConnectionconstructor failure cleanup.How It Was Tested
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make lint(fmt-maven-plugin:format).JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make unittest(Checkstyle, Java 8animal-sniffer, and the full JUnit 5 unit test suite) and integration test compilation.PerConnectionFileHandlerTest,OpenTelemetryJulHandlerTest, andBigQueryJdbcCustomLoggerTestcovering POSIX0700/0600creation and tightening, shared directory &/tmppreservation, file and directory symlink rejection, literal%paths,maxLogFileBytessystem property overrides, bounded size truncation, same-second prefix collisions, concurrentclose()vspublish()races, post-closeResultSet/detachedStruct/retry-string non-resurrection, failedBigQueryConnectionconstructor cleanup, and OTel message formatting.IT*) were not executed against production BigQuery in this step.