[Mlir-commits] [mlir] [mlir][lsp] Ensure stdin is blocking. (PR #74117)

Jacques Pienaar llvmlistbot at llvm.org
Fri Dec 1 09:41:15 PST 2023


https://github.com/jpienaar created https://github.com/llvm/llvm-project/pull/74117

Without this/if forked where non-blocking is set, the input fails as there are no retries while reading a line.

Not sure if this is a bit of a hammer, but avoids surprises depending on how server is "launched".

>From 640b70ad0d0e791aab7a1048c6c5381f1acaaf6f Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 1 Dec 2023 09:32:28 -0800
Subject: [PATCH] [mlir][lsp] Ensure stdin is blocking.

Without this/if forked where non-blocking is set, the input fails as
there are no retries while reading a line.
---
 mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp b/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp
index 259bd2613a6ccdd..3cc218aa2a527ed 100644
--- a/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp
+++ b/mlir/lib/Tools/mlir-lsp-server/MlirLspServerMain.cpp
@@ -7,6 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Tools/mlir-lsp-server/MlirLspServerMain.h"
+#include <cerrno>
+#include <cstring>
+#include <fcntl.h>
+#include <unistd.h>
 #include "LSPServer.h"
 #include "MLIRServer.h"
 #include "mlir/IR/Dialect.h"
@@ -67,6 +71,11 @@ LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
   // Configure the transport used for communication.
   llvm::sys::ChangeStdinToBinary();
   JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint);
+  if (int flags = fcntl(STDIN_FILENO, F_GETFL, 0); flags < 0) {
+    Logger::debug("Error getting fcntl flags: %s\n", strerror(errno));
+  } else if (fcntl(STDIN_FILENO, F_SETFL, flags & ~(O_NONBLOCK)) < 0) {
+    Logger::debug("Error setting blocking stding: %s\n", strerror(errno));
+  }
 
   // Register the additionally supported URI schemes for the MLIR server.
   URIForFile::registerSupportedScheme("mlir.bytecode-mlir");



More information about the Mlir-commits mailing list