Skip to content

显式库路径配置优先于系统默认搜索#12

Open
ghangz wants to merge 3 commits into
MetaX-MACA:mainfrom
ghangz:mengz/mxsml-library-path-env
Open

显式库路径配置优先于系统默认搜索#12
ghangz wants to merge 3 commits into
MetaX-MACA:mainfrom
ghangz:mengz/mxsml-library-path-env

Conversation

@ghangz

@ghangz ghangz commented Jun 25, 2026

Copy link
Copy Markdown

这个改动修正了显式库路径配置与系统默认搜索之间的优先级关系。当用户已经通过环境变量给出明确的库位置时,加载逻辑会优先遵循显式配置,避免系统路径中的旧版本库被静默选中。

对应测试已经更新并通过,覆盖了显式配置优先、生效范围以及无显式配置时的回退路径,验证结果符合预期。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for custom container layouts by allowing the library loader to resolve libmxsml.so via the MXSML_LIBRARY_PATH or MACA_HOME environment variables, falling back to default hardcoded paths if they are not set. It also adds comprehensive unit tests for this new path resolution logic. Feedback was provided regarding the priority of explicit configurations: if a user explicitly configures an environment variable, the loader should fail fast upon loading errors rather than silently falling back to default paths or allowing system-wide libraries to override the explicit configuration.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread pkg/lib/lib.go Outdated
Comment on lines 57 to 60
for _, path := range candidateLibraryPaths() {
if _, err := os.Stat(path); err == nil {
installPath = path
break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

优先使用显式配置的环境变量,并避免静默回退与系统路径覆盖问题

问题分析

  1. 系统路径覆盖环境变量:在 Load() 函数中,首先尝试了 C.dlopen("libmxsml.so")。如果系统默认路径(如 /usr/libLD_LIBRARY_PATH)中已经存在 libmxsml.so,它会直接加载成功并返回。这意味着,即使开发人员显式设置了 MXSML_LIBRARY_PATHMACA_HOME 来指定特定版本的动态库,也会被系统路径中的动态库静默覆盖,这违背了“显式配置优先”的原则。
  2. 静默回退(Silent Fallback):如果用户显式设置了 MXSML_LIBRARY_PATHMACA_HOME,但由于拼写错误、权限问题或路径不存在导致加载失败,当前逻辑会忽略该错误,并继续尝试默认的硬编码路径(如 /opt/mxdriver/lib/...)。这种静默回退会导致用户难以发现配置错误,甚至可能在不知情的情况下加载了错误版本的动态库,从而引发难以排查的兼容性问题。

建议方案
建议将路径解析分为“显式配置路径”和“默认回退路径”两部分:

  • 如果用户显式设置了 MXSML_LIBRARY_PATHMACA_HOME,则必须尝试加载该路径。如果加载失败,应直接返回错误,而不是静默回退。
  • 如果用户未设置任何环境变量,则先尝试系统默认路径(dlopen("libmxsml.so")),最后回退到默认的硬编码路径。

重构示例

pkg/lib/search_paths.go 中:

func customLibraryPath() (string, bool) {
	if value := strings.TrimSpace(os.Getenv("MXSML_LIBRARY_PATH")); value != "" {
		info, err := os.Stat(value)
		if err == nil && info.IsDir() {
			return filepath.Join(value, mxsmlLibName), true
		}
		return value, true
	}
	if value := strings.TrimSpace(os.Getenv("MACA_HOME")); value != "" {
		return filepath.Join(value, "lib", mxsmlLibName), true
	}
	return "", false
}

func defaultLibraryPaths() []string {
	return []string{
		"/opt/mxdriver/lib/" + mxsmlLibName,
		"/opt/maca/lib/" + mxsmlLibName,
		"/opt/mxn100/lib/" + mxsmlLibName,
	}
}

pkg/lib/lib.goLoad() 中:

func Load() error {
	g_mxsmlLib.mu.Lock()
	defer g_mxsmlLib.mu.Unlock()

	if g_mxsmlLib.loaded {
		return nil
	}

	dlopenPath := func(path string) (unsafe.Pointer, error) {
		cPath := C.CString(path)
		defer C.free(unsafe.Pointer(cPath))
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()
		handle := C.dlopen(cPath, C.int(g_mxsmlLib.flag))
		if handle == nil {
			return nil, getDlError()
		}
		return handle, nil
	}

	// 1. 优先使用显式配置的环境变量,加载失败直接报错,不进行回退
	if customPath, ok := customLibraryPath(); ok {
		handle, err := dlopenPath(customPath)
		if err != nil {
			return fmt.Errorf("failed to load custom mxsml lib from %s: %w", customPath, err)
		}
		g_mxsmlLib.handle = handle
		g_mxsmlLib.loaded = true
		return nil
	}

	// 2. 未配置环境变量时,尝试系统默认路径
	if handle, err := dlopenPath(mxsmlLibName); err == nil {
		g_mxsmlLib.handle = handle
		g_mxsmlLib.loaded = true
		return nil
	}

	// 3. 最后尝试默认的硬编码路径
	for _, path := range defaultLibraryPaths() {
		if _, err := os.Stat(path); err == nil {
			if handle, err := dlopenPath(path); err == nil {
				g_mxsmlLib.handle = handle
				g_mxsmlLib.loaded = true
				return nil
			}
		}
	}

	return fmt.Errorf("invalid mxsml lib path")
}

@ghangz ghangz changed the title 支持通过环境变量指定 mxsml 动态库路径 ???????????????? Jun 26, 2026
@ghangz ghangz changed the title ???????????????? 显式库路径配置优先于系统默认搜索 Jun 26, 2026
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.

1 participant