Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clojure nREPL connection then eval race condition leads to broken REPL #396

Open
Invertisment opened this issue Aug 15, 2022 · 2 comments
Open
Assignees
Labels
client-clojure enhancement New feature or request

Comments

@Invertisment
Copy link

Invertisment commented Aug 15, 2022

I have developed a plugin that adds some middleware. But if I try to call server.with-conn-and-ops-or-warn then it doesn't crash when the REPL isn't connected. So the call either results in a NOOP or it succeeds. It's random.

So I would like to have some kind of a way to know that it succeeded.

The callback would also be useful when Conjure decides to reconnect to a different REPL. It could notify the child plugins about it.

The alternative could be that I would try to brute-force that function until it "connects" but it's not a good thing to do.

@Olical
Copy link
Owner

Olical commented Aug 17, 2022

The function takes a opts table which can contain an :else function which will be called if there is no current connection. You could use that to schedule a retry until the REPL is connected. This is pretty internal and undocumented though so YMMV over time if I end up refactoring this code one day. Should be stable for a long time though.

I think you'll just have to check the state of the client and inspect the REPL it's connected to if you want to know if you're still connected to the same one. Not planning on adding plugin-plugin support (plugins for Conjure as a first class thing), I'd rather they piggieback on what is already there or are added to the core through pull requests etc. Some core changes can be made to accommodate plugins but I don't plan on adding notification systems, hooks, extra APIs and deep internal documentation for every internal function (there's a LOT of code I've written over years and it changes, it'd be really hard to maintain that right now).

You can also use this to see if there's a connection

(defn connected? []
(if (state.get :conn)
true
false))

Or inspect the state and get the current connection like so:

(let [conn (state.get :conn)]

If you're going to be reaching into the internals of Conjure and poking things, might as well reach deeper and access the data you're interested in. It's all there, you have access to it, just have to dig through the code a little to see how I'm doing it. Shouldn't need anything extra added to accommodate further plugins (although I'd probably refer to them as mods since they're piggiebacking and modifying Conjure while it's running?).

@Invertisment
Copy link
Author

Invertisment commented Aug 17, 2022

I already did what you described (unfortunately the connected? function was private so I copied the code):

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/additional-fns.fnl#L47-L52

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/additional-fns.fnl#L55-L56

Even the timer:

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/load-util.fnl#L8-L24

The only problematic thing is that sometimes the test middleware fails to load and then I must restart the REPL as even the editor restarts don't help. That I can't understand yet.

Also this timer loading mechanism sometimes produces multiple error messages when it fails to connect and I think it's somehow related to vim.schedule_wrap. When I jump through buffers several times rapidly then it produces some errors as the timer still goes on.

I'll try the :else option. It will also be handy (if related) to probably detect when test namespace is not found (there is no way to do it if I use nREPL to run tests because I use test namespace lookup matcher and I output "ok/not ok" in the command field instead of the log).

@Olical Olical added enhancement New feature or request client-clojure labels Aug 24, 2022
@Olical Olical self-assigned this Sep 18, 2024
@Olical Olical pinned this issue Sep 18, 2024
@Olical Olical changed the title Wait until a nREPL connection is created? Clojure nREPL connection then eval race condition leads to broken REPL Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client-clojure enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants