Skip to content

Removing check for writable file - #650

Closed
viniciusccarvalho wants to merge 1 commit into
ev3dev-lang-java:masterfrom
viniciusccarvalho:Sysfs-checkPermission
Closed

viniciusccarvalho wants to merge 1 commit into
ev3dev-lang-java:masterfrom
viniciusccarvalho:Sysfs-checkPermission

Conversation

@viniciusccarvalho

@viniciusccarvalho viniciusccarvalho commented Feb 24, 2019 •

Copy link
Copy Markdown

Seems that file.isWritable() is unreliable depending on the OS/JDK version.
The catch clause of the block would capture if the file is really not writable anyways.
#651

Seems that file.isWritable() is unreliable depending on the OS/JDK version.
The catch clause of the block would capture if the file is really not writable anyways.
@viniciusccarvalho

Copy link
Copy Markdown
Author

See #410 #554

@jabrena

jabrena commented Feb 24, 2019

Copy link
Copy Markdown
Member

Hi, do you think this is the source of the problem for the motor part?

@viniciusccarvalho

Copy link
Copy Markdown
Author

It solved the problem here. The fact that an exception will be thrown anyways, and where most of the times the file should be writable, the check is redundant.

There are a few reports of canXXXX methods being broken on JDK depending on the version/OS

Read here: https://stackoverflow.com/questions/10783677/how-to-check-file-permissions-in-java-os-independently

And here:

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6203387

@JakubVanek

JakubVanek commented Feb 24, 2019 •

Copy link
Copy Markdown
Contributor

Hi,

I believe that this is not the cause of the motor issue. But the permission check probably is redundant as the exception is going to be caught and printed out by the catch block below it.

canWrite() on Linux (the only OS this library natively supports) according to #554 (comment) boils down to basically POSIX access(path, W_OK). This should work even with ACLs, if they were used.

I think this problem are caused by having a race condition with udev, a device manager daemon on Linux. Files on /sys filesystem (which serves as one way how to access system devices) aren't by default writable by ordinary users. This is overriden by a udev rule which gives access also to the default ev3dev user. However udev lives in the userspace and there is a certain delay before the rules take effect. If there is a change that adds something in /sys, there is a time window when a device can be found in the directory structure, yet its attributes cannot be accessed.

According to the following part of this comment...

I have also noticed that my motors on the filesystem kept changing from mapped folders (motor0, motor2, motor8, motor7) I did not take notes if there was a correlation between the mount folder and when the code worked.

...something is triggering a change in /sys. One such thing may be unplugging and plugging back the affected device. It might be also something in the library when it switches port modes (e.g. UnregulatedMotor).

In some places there is a workaround: a fixed delay which should cover most of the cases.

If the hypothesis about the udev race-condition is correct, the proper solution to this is to wait until udev realizes that there is a new device and it applies its rules. Yesterday I tried implementing a sensor/motor "backend" via udev-java-bindings and it seems viable, However (and I hope it is only due to JNA) the startup performance quite suffered. The next step is to reimplement it via JNI. However I do not want to promise anything.

Regards,

Jakub

@viniciusccarvalho

Copy link
Copy Markdown
Author

@JakubVanek the "mount" change was not happening due motors being reconnected, that's what lead me to make this comment. I would assume that a change in the port mapping to be normal on reconnect, but I have observed that during redeployment of my app, the motors would change ports.

Your suspicion on race condition, I have also noticed that this error happens as part of the constructor of the BaseRegulatedMotor, although there's quite a few delays on the constructor.

Would make sense to move the reset piece away from the constructor then? I don't fully understand the udev mapping, but wouldn't make sense to move this away from the constructor? In my case I build two motors and they sit idle until a good chunk of time has passed.

Perhaps turning the reset in to a lazy property that is checked upon a first command request (if the request is necessary to be executed before any command)

Maybe that can also reduce the chances of racing conditions

@jabrena

jabrena commented Feb 24, 2019

Copy link
Copy Markdown
Member

Please @viniciusccarvalho create an issue to continue this technical conversation.

@JakubVanek, we spoke about udev, previously. It is not necessary.

@jabrena

jabrena commented Feb 25, 2019

Copy link
Copy Markdown
Member

We will continue the discussion in the issue.
I will close this PR first.

@jabrena jabrena closed this Feb 25, 2019
@JakubVanek

Copy link
Copy Markdown
Contributor

Hmm, if I look at this from a theoretical point of view, it is be better to check the failure with exception type. That will make it a bit more atomic, i.e. opening and checking the permission should happen in one step in the open() syscall.

@JakubVanek JakubVanek reopened this Feb 26, 2019
JakubVanek added a commit that referenced this pull request Mar 14, 2019
This commit makes some refactorings in Sysfs:

 - #650 is
   integrated. Together with this, the writeString() now
   manually encodes the string to bytes and writes it using
   FileOutputStream. This should ensure the best performance.

 - readString() is a bit tweaked to use exceptions in a better way.

 - getElements() is checking for null value of a result.

 - write* method do not return a boolean anymore. This is
   because I think exceptions already convey that an error occured.
   The only usage of this boolean was to throw an exception.

 - Slf4j formatting functions are used, so it is not necessary to
   check if tracing is enabled. Slf4j already checks if the level is
   enabled before the format is processed.
@JakubVanek

Copy link
Copy Markdown
Contributor

I have built another PR on top of this one in #652 - it includes some more Sysfs refactoring.

JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- change writeString() to use FileOutputStream and
  better exception handling. This could also make it
  a little bit more faster.
  This also integrates #650.

- change writeInteger() to use Integer.toString().

- change readString to use better exception handling.

- change getElements() to check for null return value.
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- change writeString() to use FileOutputStream and
  better exception handling. This could also make it
  a little bit more faster.
  This also integrates #650.
  This avoids the use of File.isWritable() function,
  instead an exception will be catched and logged.

- change writeInteger() to use Integer.toString().

- change readString to use better exception handling.

- change getElements() to check for null return value.

Co-authored-by: Vinicius Carvalho <viniciusccarvalho@gmail.com>
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- change writeString() to use FileOutputStream and
  better exception handling. This could also make it
  a little bit more faster.
  This also integrates #650.
  This avoids the use of File.isWritable() function,
  instead an exception will be catched and logged.

- change writeInteger() to use Integer.toString().

- change readString to use better exception handling.

- change getElements() to check for null return value.

Co-authored-by: Vinicius Carvalho <viniciusccarvalho@gmail.com>
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- change writeString() to use FileOutputStream and
  better exception handling. This could also make it
  a little bit more faster.
  This also integrates #650.
  This avoids the use of File.isWritable() function,
  instead an exception will be catched and logged.

- change writeInteger() to use Integer.toString().

- change readString to use better exception handling.

- change getElements() to check for null return value.

Co-authored-by: Vinicius Carvalho <viniciusccarvalho@gmail.com>
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- change writeString() to use FileOutputStream and
  better exception handling. This could also make it
  a little bit more faster.
  This also integrates #650.
  This avoids the use of File.isWritable() function,
  instead an exception will be catched and logged.

- change writeInteger() to use Integer.toString().

- change readString to use better exception handling.

- change getElements() to check for null return value.

Co-authored-by: Vinicius Carvalho <viniciusccarvalho@gmail.com>
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- Changes to writeString() and readString() to use lower-level IO
  This could make it a little faster.

- better exception handling - inspired by #650

- change writeInteger() to use Integer.toString().

- change getElements() to check for null return value.
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- Changes to writeString() to use lower-level IO
  This could make it a little faster.

- better exception handling - inspired by #650

- change writeInteger() to use Integer.toString().

- change getElements() to check for null return value.
JakubVanek added a commit that referenced this pull request Mar 14, 2019
- Slf4j checks for log level are removed and formatting
  log function overloads are used. This way Slf4j does
  the checking on its own.

- Changes to writeString() to use lower-level IO
  This could make it a little faster.

- better exception handling - inspired by #650

- change writeInteger() to use Integer.toString().

- change getElements() to check for null return value.
@JakubVanek JakubVanek mentioned this pull request May 30, 2019
5 tasks done
@JakubVanek

Copy link
Copy Markdown
Contributor

This PR is mostly a refactoring - it prevents a double checking of file permissions. (actually, python has a nice name for this: better ask forgiveness than permission) As such, there is probably no concrete reproducer.

To verify that it still works, try to create a BaseRegulatedMotor subclass instance (that should call writeString() at least once).

To check the thrown permission-denied-exception, remove the delays in BaseRegulatedMotors and before running the program, unplug and replug the motor into the brick.

@JakubVanek

Copy link
Copy Markdown
Contributor

However it seems that #654 reworks this.

@JakubVanek JakubVanek self-assigned this Sep 25, 2019

@JakubVanek JakubVanek left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code follows the better ask forgiveness than permission model
EDIT: the permission exception is catched below anyway and that codepath also returns false.

@jabrena jabrena modified the milestones: v1.1.0, v1.0.0 Dec 2, 2019
@jabrena

jabrena commented Dec 2, 2019

Copy link
Copy Markdown
Member

We are not continue with this PR.

@jabrena jabrena closed this Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants