Class: Steep::Daemon::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/daemon/server.rb

Defined Under Namespace

Classes: FileTracker

Constant Summary collapse

FILE_CHANGE_TYPE =

LSP file change types (from Language Server Protocol specification)

{
  created: 1,
  changed: 2,
  deleted: 3
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, project:, stderr:) ⇒ Server

Returns a new instance of Server.



84
85
86
87
88
89
90
91
92
# File 'lib/steep/daemon/server.rb', line 84

def initialize(config:, project:, stderr:)
  @config = config
  @project = project
  @stderr = stderr
  @shutdown_flag = false
  @file_tracker = FileTracker.new
  @warmup_status = :not_started  # :warming_up, :ready, :failed
  @warmup_mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/steep/daemon/server.rb', line 6

def config
  @config
end

#file_trackerObject (readonly)

Returns the value of attribute file_tracker.



7
8
9
# File 'lib/steep/daemon/server.rb', line 7

def file_tracker
  @file_tracker
end

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/steep/daemon/server.rb', line 6

def project
  @project
end

#shutdown_flagObject (readonly)

Returns the value of attribute shutdown_flag.



7
8
9
# File 'lib/steep/daemon/server.rb', line 7

def shutdown_flag
  @shutdown_flag
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/steep/daemon/server.rb', line 6

def stderr
  @stderr
end

#warmup_mutexObject (readonly)

Returns the value of attribute warmup_mutex.



8
9
10
# File 'lib/steep/daemon/server.rb', line 8

def warmup_mutex
  @warmup_mutex
end

#warmup_statusObject (readonly)

Returns the value of attribute warmup_status.



8
9
10
# File 'lib/steep/daemon/server.rb', line 8

def warmup_status
  @warmup_status
end

Instance Method Details

#runObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/steep/daemon/server.rb', line 94

def run
  Steep.logger.info { "Steep server starting for #{Dir.pwd}" }
  Steep.logger.info { "PID: #{Process.pid}" }

  client_read, server_write = IO.pipe
  server_read, client_write = IO.pipe

  client_reader = LanguageServer::Protocol::Transport::Io::Reader.new(client_read)
  client_writer = LanguageServer::Protocol::Transport::Io::Writer.new(client_write)
  server_reader = LanguageServer::Protocol::Transport::Io::Reader.new(server_read)
  server_writer = LanguageServer::Protocol::Transport::Io::Writer.new(server_write)

  job_count = (ENV["STEEP_SERVER_JOB_COUNT"] || [Etc.nprocessors - 1, 1].max).to_i

  Steep.logger.info { "Starting #{job_count} typecheck worker(s)..." }

  workers = ::Steep::Server::WorkerProcess.start_typecheck_workers(
    steepfile: @project.steepfile_path,
    args: [],
    delay_shutdown: true,
    steep_command: nil,
    count: job_count
  )

  interaction_worker = ::Steep::Server::WorkerProcess.start_worker(
    :interaction,
    name: "interaction",
    steepfile: @project.steepfile_path,
    steep_command: nil
  )

  master = ::Steep::Server::Master.new(
    project: @project,
    reader: server_reader,
    writer: server_writer,
    interaction_worker: interaction_worker,
    typecheck_workers: workers,
    refork: true
  )
  master.typecheck_automatically = false

  master_thread = Thread.start do
    Thread.current.abort_on_exception = true
    master.start
  end

  Steep.logger.info { "Initializing (loading RBS environment)..." }
  init_id = SecureRandom.alphanumeric(10)
  client_writer.write(method: :initialize, id: init_id, params: {})
  wait_for_response(client_reader, init_id)

  all_paths = collect_all_project_paths
  @file_tracker.register(all_paths)

  Steep.logger.info { "Server ready. Tracking #{all_paths.size} files." }
  Steep.logger.info { "Socket: #{@config.socket_path}" }

  # SAFE: Verify socket path is actually a socket before deleting (prevents symlink attacks)
  if File.exist?(@config.socket_path)
    unless File.socket?(@config.socket_path)
      raise "#{@config.socket_path} exists but is not a socket (possible symlink attack)"
    end
    File.delete(@config.socket_path)
  end
  @unix_server = UNIXServer.new(@config.socket_path)
  # SAFE: Restrict socket access to owner only (prevents unauthorized connections)
  File.chmod(0600, @config.socket_path)

  warmup_thread = Thread.new do
    Thread.current.abort_on_exception = false
    set_warmup_status(:warming_up)
    stderr.puts "Warming up type checker (loading gem signatures and RBS files)..."
    warm_typecheck_on_startup(client_writer, client_reader)
    stderr.puts "Warm-up complete. Ready for fast type checking."
    set_warmup_status(:ready)
  rescue StandardError => e
    Steep.logger.error { "Warm-up error: #{e.class}: #{e.message}" }
    Steep.logger.debug { e.backtrace&.first(10)&.join("\n") }
    set_warmup_status(:failed)
  end

  watcher_thread = start_background_watcher(client_writer, client_reader)

  server = @unix_server or raise

  Signal.trap("TERM") { @shutdown_flag = true; server.close rescue nil }
  Signal.trap("INT")  { @shutdown_flag = true; server.close rescue nil }

  until @shutdown_flag
    begin
      ready = IO.select([server], nil, nil, 1) # steep:ignore UnresolvedOverloading
      next unless ready

      client_socket = server.accept

      unless warmup_ready?
        sleep 0.1 until warmup_ready? || @shutdown_flag
      end

      next if @shutdown_flag

      handle_client(client_socket, client_writer, client_reader)
    rescue IOError, Errno::EBADF
      break if @shutdown_flag
      raise
    rescue StandardError => e
      Steep.logger.error { "Error handling client: #{e.class}: #{e.message}" }
      Steep.logger.debug { e.backtrace&.first(10)&.join("\n") }
    ensure
      client_socket&.close rescue nil
    end
  end

  Steep.logger.info { "Shutting down..." }
  warmup_thread&.kill
  watcher_thread&.kill
  shutdown_master(client_writer, client_reader)
  master_thread.join(10)
rescue StandardError => e
  Steep.logger.fatal { "Fatal error: #{e.class}: #{e.message}" }
  Steep.logger.error { e.backtrace&.join("\n") }
ensure
  Daemon.cleanup
  Steep.logger.info { "Server stopped." }
end