jason810496 commented on code in PR #70085:
URL: https://github.com/apache/airflow/pull/70085#discussion_r3635807346


##########
ts-sdk/src/coordinator/log-channel.ts:
##########
@@ -66,22 +67,39 @@ export class LogChannel {
     this.isRoot = isRoot;
   }
 
+  /** Create a root channel with no socket yet; records are buffered
+   *  until {@link LogChannel#connect} flushes them. */
+  static createBuffered(name: string = DEFAULT_LOGGER_NAME): LogChannel {
+    return new LogChannel({ sock: null, connected: false, closed: false, 
buffer: [] }, name, true);
+  }
+
   static async connect(addr: string, name: string = DEFAULT_LOGGER_NAME): 
Promise<LogChannel> {

Review Comment:
   I lean toward dropping the `LogChannel.connect()`. WDYT?
   
   ---
   
   After this change the static `LogChannel.connect()` has no production 
callers — `runtime.ts` uses `createBuffered()` + instance `connect()` — and it 
now shares the name `connect` with an instance method of a different shape 
(factory returning a channel vs. mutator). Consider either dropping it and 
having tests use the same two-step API production uses, or noting in its doc 
comment that it's a test convenience. No suggestion block since removing it 
touches ~10 test call sites.



##########
ts-sdk/src/coordinator/log-channel.ts:
##########
@@ -66,22 +67,39 @@ export class LogChannel {
     this.isRoot = isRoot;
   }
 
+  /** Create a root channel with no socket yet; records are buffered
+   *  until {@link LogChannel#connect} flushes them. */
+  static createBuffered(name: string = DEFAULT_LOGGER_NAME): LogChannel {
+    return new LogChannel({ sock: null, connected: false, closed: false, 
buffer: [] }, name, true);
+  }
+
   static async connect(addr: string, name: string = DEFAULT_LOGGER_NAME): 
Promise<LogChannel> {
-    const shared: LogChannelState = {
-      sock: await connectTcp(addr),
-      connected: true,
-      closed: false,
-    };
-    shared.sock.on("error", (err) => {
+    const channel = LogChannel.createBuffered(name);
+    await channel.connect(addr);
+    return channel;
+  }
+
+  /** Connect the socket and flush buffered records, in order. Root only. */
+  async connect(addr: string): Promise<void> {
+    if (!this.isRoot) return;
+    const shared = this.shared;
+    const name = this.name;
+    const sock = await connectTcp(addr);
+    shared.sock = sock;
+    shared.connected = true;

Review Comment:
   `connect()` has no state guards, which leaves two latent holes (neither 
reachable from `runtime.ts` today, but this is now the class's main entry 
point):
   
   1. **Root-only no-op is silent**: `await child.connect(addr)` resolves 
successfully while connecting nothing — the socket stays `null` and every 
record buffers indefinitely. The doc comment says "Root only"; throwing encodes 
that loudly. (The same guard in `close()` is fine — children legitimately don't 
own the socket.)
   2. **No `closed`/double-connect check**: a `close()` that runs while 
`connectTcp` is in flight dumps the buffer and returns (sock is still `null`), 
then `connect()` resolves and adopts a socket nobody will ever close — it keeps 
the event loop alive. A second `connect()` call would also orphan the first 
socket, whose stale `close` handler later flips `shared.connected = false` and 
diverts all logs from the healthy socket to stderr.
   
   ```suggestion
     async connect(addr: string): Promise<void> {
       if (!this.isRoot) {
         throw new Error(`[${this.name}] connect() is root-only`);
       }
       if (this.shared.sock) {
         throw new Error(`[${this.name}] connect() called more than once`);
       }
       const shared = this.shared;
       const name = this.name;
       const sock = await connectTcp(addr);
       if (shared.closed) {
         // close() won the race while we were dialing; it already dumped the
         // buffered records to stderr, so just discard the socket.
         sock.destroy();
         return;
       }
       shared.sock = sock;
       shared.connected = true;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to